constructor - PHP: Can I pass $this in the _constructor? -
i pass php object in own constructor object this:
class foo { $parent_object; public function __construct($obj) { $this->parent_object = $obj; } } class bar { public function __construct() { $blub = new foo($this); } }
the question asking myself is: can pass $this in constructor of bar, because object has not been created... $this valid reference whole object?
sure can. maintain in mind $this
points current object though. in:
$blub = new foo($this);
$this
points instance of bar
. $obj
in constructor of foo
instance of bar
.
$this
available @ origin of constructor. if bar
calls functions on $obj
might instance of bar
not yet in right state (ie. $blub
has not been assigned). lead reference issues, solution move reference exchange out of constructor.
php constructor this
No comments:
Post a Comment