PHP method chaining
<?php
// REF: http://php.net/manual/en/language.references.return.php
<?php
class Foo {
protected $bar; // this is where the instance of Bar will be stored
public function __construct() {
// get instance of Bar, since this is the first line to be executed and calls the Bar constructor
// it is going to first print "Bar"
$this->bar = new Bar();
print "Foo\n"; // after Bar this is what is printed
}
public function getBar() {
// return an instance of Bar()
// return new Bar() , first prints "Bar"
return $this->bar;
}
}
// ----------------------------------------
class Bar {
public function __construct() {
print "Bar\n";
}
public function helloWorld() {
print "Hello World\n";
}
}
function test() {
return new Foo();
}
// foo_object->getBar() -> helloWorld() Bar Foo
// bar_object -> helloWorld() Bar Foo Hello World
// Hello World
test()->getBar()->helloWorld();
// RESULT: Bar Foo Hello World