<?php class Tes { private $name; public function __construct($name) { $this->name = $name; } public function append($string) { $this->name .= $string; } }
class Test { private $tes; public function add($name) { $tes = new Tes($name); $this->tes = $tes; return $tes; } } $test = new Test; $test->add('Foo')->append('Baz'); var_dump($test);
I’m surprised to know that the Tes class which is inside Test class has a name FooBaz.
I think that it will just Foo because $test->add('Foo')
evaluate first before append('Baz')
Is the returned object $tes
a reference to $test->tes
, so any change made to the returned object will also affect the property that has just been set ?