note 78123 added to language.references.return
| From: | stanlemon at mac dot com | Date: | Fri, 28 Sep 2007 17:51:42 +0000 |
| Subject: | note 78123 added to language.references.return | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-130886@lists.php.net to get a copy of this message | ||
I haven't seen anyone note method chaining in PHP5. When an object is returned by a method in
PHP5 it is returned by default as a reference, and the new Zend Engine 2 allows you to chain method
calls from those returned objects. For example consider this code:
<?php
class Foo {
protected $bar;
public function __construct() {
$this->bar = new Bar();
print "Foo\n";
}
public function getBar() {
return $this->bar;
}
}
class Bar {
public function __construct() {
print "Bar\n";
}
public function helloWorld() {
print "Hello World\n";
}
}
function test() {
return new Foo();
}
test()->getBar()->helloWorld();
?>
Notice how we called test() which was not on an object, but returned an instance of Foo, followed by
a method on Foo, getBar() which returned an instance of Bar and finally called one of its methods
helloWorld(). Those familiar with other interpretive languages (Java to name one) will recognize
this functionality. For whatever reason this change doesn't seem to be documented very well,
so hopefully someone will find this helpful.
----
Server IP: 64.71.164.2
Probable Submitter: 72.22.26.23
----
Manual Page -- http://www.php.net/manual/en/language.references.return.php
Edit -- https://master.php.net/note/edit/78123
Del: integrated -- https://master.php.net/note/delete/78123/integrated
Del: useless -- https://master.php.net/note/delete/78123/useless
Del: bad code -- https://master.php.net/note/delete/78123/bad+code
Del: spam -- https://master.php.net/note/delete/78123/spam
Del: non-english -- https://master.php.net/note/delete/78123/non-english
Del: in docs -- https://master.php.net/note/delete/78123/in+docs
Del: other reasons-- https://master.php.net/note/delete/78123
Reject -- https://master.php.net/note/reject/78123
Search -- https://master.php.net/manage/user-notes.php