Jump to content

Use The Same Instance Of A Class In Multiple Files


Turbine

Recommended Posts

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. :)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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();

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.