Ryan Smith
Ryan Smith

Reputation: 694

Calling classes from all other classes - PHP

So I have a config.php file, wich is included on top of every page on the site before the tag. This file uses __autoload() to get all the php classes I use. Then, after autoloading them I assign the classes to variables like so...

$myclass = new Myclass();
$mysecondclass = new MySecondClass();

When I want to call $mysecondclass in $myclass, I get an undefined variable error. This is of course, because $mysecondclass was not defined before $myclass. How do I fix this where I can define all classes from any other class?

Thanks for any help:)

Upvotes: 1

Views: 138

Answers (4)

SimDion
SimDion

Reputation: 1080

According to how your classes works, think about extending them... Extending MyClass to MySecondClass automatically gives access to parent properties / functions.

MyClass

class MyClass{
    function myFunction(){
         // code here
    }
}

MySecondClass

class MySecondClass extends MyClass{
    function __construct(){
         $this->myFunction();
    }
}

Upvotes: 0

Max
Max

Reputation: 76

The registry pattern is also an easy way to access "global" object anywhere in your code.

You can Take a look at Zend_Registry : http://framework.zend.com/manual/en/zend.registry.using.html

$myclass = new Myclass();
$mysecondclass = new MySecondClass();
Zend_Registry::set('mysecondclass', $mysecondclass);
$myclass->doSomething();

class Myclass
{
    public function doSomething()
    {
        // use $mysecondclass via the registry
        Zend_Registry::get('mysecondclass')->methodFromSecondClass();
    }
}

class MySecondClass
{
    public function methodFromSecondClass()
    {
        return true;        
    }
}

Upvotes: 0

Okonomiyaki3000
Okonomiyaki3000

Reputation: 3696

$myclass is not Myclass it is an instance of Myclass (an object). Objects are not classes. If you want to use a new instance of MySecondClass inside of an instance of Myclass, just instantiate it in there.

Your question also deals with scope. You have created these two objects in the global scope. They are not automatically accessible within one another. If you have some object which you will have only one instance of and which you want to be global and accessible by others, you can import it into their scope with the global keyword. However, this is not usually the best way to go about it. Instead, you may want to look up the Singleton Pattern. It gives you a way to ensure that a particular class is only instantiated one time and gives you a way to access the object that has been created as a result of that instantiation.

Upvotes: 0

Broncha
Broncha

Reputation: 3794

The best OOP approach would be to use a superObject with properties which are objects of other classes.

class Context{
  var $myClass;

  var $myOtherClass;

  var $db;

  //...
}

In your MyClass

class MyClass{

  var $context;

  public function __construct(&context){
    $this->context = $context;
  }

  public function otherFunction(){
    $this->context->myOtherClass->functionFromOtherClass();
  }
}

You should be instantiating these classes using Factory method or any mechanism to manage objects.

To initialize MyClass you would implement something like this.

ObjectFactory::createObject('MyClass',$context);

Upvotes: 4

Related Questions