mardi 19 septembre 2017

pass an object to another object constructor in PHP

I am reading about design patterns in PHP and I keep seeing the following syntax, for example

$newClass = new myClass(new myOtherClass());

I guess that this passes some kind of a myOtherClass reference inside myClass, so here

class myClass{

    private $myOtherClass;

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

    public function myClassMethod($var) {
        $this->myOtherClass->methodOfMyOtherClass($var);
    }
}

the construct refers to myOtherClass and I can use the methodOfMyOtherClass of myOtherClass from the myClass myClassMethod, like so $newClass->myClassMethod('a value here');

My questions are :

What is the name of that practice?

Did I got the concept right?

Where can I read more about it?

(first time I saw this syntax is here, this is where I also based my code for this question)

Thank you

Aucun commentaire:

Enregistrer un commentaire