Turbine Posted September 25, 2012 Share Posted September 25, 2012 First off I really am into C++, using PHP for me is a bit odd as things are done quite differently and loosely. I'm instantiating my classes (Query & Model) at the end of each script, I've been using require_once() in each script which requires these objects. As they have already been included the script won't run, but the thing I find most strange is even though it's already been included, the variables are not accessible. Is there a way to use the same instance in a variable in all other files which use these? Thanks, if there's another way I should achieve this I'm open to suggestions. Quote Link to comment https://forums.phpfreaks.com/topic/268770-use-the-same-instance-of-a-class-in-multiple-files/ Share on other sites More sharing options...
requinix Posted September 25, 2012 Share Posted September 25, 2012 Instantiating them at the end of your scripts? Do you have an example of what you mean by that? Quote Link to comment https://forums.phpfreaks.com/topic/268770-use-the-same-instance-of-a-class-in-multiple-files/#findComment-1380760 Share on other sites More sharing options...
Turbine Posted September 25, 2012 Author Share Posted September 25, 2012 In the class file: <?PHP class Query extends DB { function __construct() { parent::__construct(); } ... } $query = new Query(); ?> Which I can see isn't going to work when including. =S Not just from already including but it will be a completely different object. Quote Link to comment https://forums.phpfreaks.com/topic/268770-use-the-same-instance-of-a-class-in-multiple-files/#findComment-1380762 Share on other sites More sharing options...
Turbine Posted September 25, 2012 Author Share Posted September 25, 2012 I took a wild stab in the dark and used global as well as accessing local class data members using $this->var. Seems like it could work. Quote Link to comment https://forums.phpfreaks.com/topic/268770-use-the-same-instance-of-a-class-in-multiple-files/#findComment-1380764 Share on other sites More sharing options...
ignace Posted September 25, 2012 Share Posted September 25, 2012 It will work when included. require 'Query.php'; $query->foo(); Another way of achieving this is at the end of Query.php (replacing the current last line) you write return new Query(); Then in your main: $query = require 'Query.php'; I must mention though that it is considered bad practice to create an instance at the end of your files. Much better is: require 'Query.php'; $query = new Query(); Because in these simple examples this works, but in bigger projects multiple instances have to be created which would result in "Cannot redeclare existing class Query" which would mean additional code would have to be wrapped around your class. Not to mention that most projects use autoloading and your instances would not even be in a global context but in the autoloader's and you would have no way to access those variables. To give an example of such an autoloader: spl_autoload_register(function($className) { $className = ltrim($className, '\\'); $fileName = ''; $namespace = ''; if ($lastNsPos = strripos($className, '\\')) { $namespace = substr($className, 0, $lastNsPos); $className = substr($className, $lastNsPos + 1); $fileName = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR; } $fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php'; require $fileName; }); $query = new Query(); As you can see the require() call is made in the autoloader and not available to you in the global context. Quote Link to comment https://forums.phpfreaks.com/topic/268770-use-the-same-instance-of-a-class-in-multiple-files/#findComment-1380765 Share on other sites More sharing options...
ignace Posted September 25, 2012 Share Posted September 25, 2012 I took a wild stab in the dark and used global as well as accessing local class data members using $this->var. Seems like it could work. It's also best to avoid global variables as much as possible. For example because PHP does not have a main() method but it does have an index.php which is auto called it is common to find a Bootstrap class in there. require '../libs/MyApp/Bootstrap.php'; (new Bootstrap)->init(); Inside the init() method you have your bootstrapping code. init() self is a template method. $this->initConfig() ->initDb() ->initFoo() ->initBar(); Quote Link to comment https://forums.phpfreaks.com/topic/268770-use-the-same-instance-of-a-class-in-multiple-files/#findComment-1380766 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.