3

I want to get an instance of an existing object. How do I do this without having to pass the object as a parameter? This is what I'm doing now:

class clas1 {
    public $myArray;

    public function setArray($a){
        $this->myArray = $a;
    }
}

class clas2 {
    public $test;

    function __construct($obj) {
        $this->test = $obj;
    }
}

$c = new clas1;
$c->setArray(array('firstname'=>'Fred'));

$c2 = new clas2($c);

var_dump($c2);

The output of var_dump is:

object(clas2)[2]
  public 'test' => 
    object(clas1)[1]
      public 'myArray' => 
        array
          'firstname' => string 'Fred' (length=4)

I suppose I could have each object as a property of a parent object, but is are there any other suggestions?

5
  • 3
    What is wrong with this way? Depending on the actual things which you need to do with the clas1 instance you may or may not need to store a link to the instance in a clas2 property. Commented Nov 1, 2011 at 14:53
  • 3
    The way you're doing it now is called Dependency Injection (en.wikipedia.org/wiki/Dependency_injection), and is actually often the preferred method. Commented Nov 1, 2011 at 14:53
  • I want to be able to use the methods and access the current values of properties. Is there no way to get an existing instance by doing something like $test =& new $clas1? Like getting a reference? Pardon my ignorance... I'm just getting my head round OOP. It seems 'clunky' to have to pass the object. Commented Nov 1, 2011 at 15:15
  • 1
    It actually is passing a reference, not copying a object instance, so it is not clunky. You would need to use & to pass objects by reference in php4 not 5. Commented Nov 1, 2011 at 15:26
  • I thought passing by reference was a bit like a global variable - that's the kind of functionality I was hoping for. What do you think of garvey's answer below - does that accomplish the same thing by forcing only one instance? Commented Nov 1, 2011 at 15:56

2 Answers 2

2

Pulling objects by calling static methods (like in singleton) is a bad practice (or even an anti-pattern). Singleton and static classes feel natural for newcomers because they work the same way we learned in the procedural paradigm, but you should avoid them (only use static classes when they have no state)

You should totally pass the object as a parameter, to avoid introducing implicit dependencies, and do good OOP.

Read about Dependency Injection.

If object creation becomes complex or annoying, you can always use a Dependency Injection Container

Sign up to request clarification or add additional context in comments.

Comments

0

You should use sigleton pattern:

class class1
{
    private static $_instance;
    public static function getInstance()
    {
        if (is_null(self::$_instance)) {
            self::$_instance = new self;
        }
        return self::$_instance;
    }
    private function __construct() {}
    private function __clone() {}
}

class class2 {
    public function __construct()
    {
        $obj = class1::getInstance();
    }
}

$a = class1::getInstance();
$b = new class2();

6 Comments

Owen didn't say anything about needing to have only one instance of clas1
This solution matches what I am trying to achieve.
What is the reason for the empty __construct() and __close() functions?
You can make it non-empty, but they should be private for guarantee of 1 instance.
@happydeveloper, can you explain why singleton is antipattern?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.