List Info

Thread: note 78123 added to language.references.return




note 78123 added to language.references.return
user name
2007-09-28 12:51:42
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 "Foon";
	}	
	
	public function getBar() {
		return $this->bar;
	}
}

class Bar {

	public function __construct() {
		print "Barn";
	}
	
	public function helloWorld() {
		print "Hello Worldn";
	}
}

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.retu
rn.php
Edit        -- https://master
.php.net/note/edit/78123
Del: integrated  -- h
ttps://master.php.net/note/delete/78123/integrated
Del: useless     -- http
s://master.php.net/note/delete/78123/useless
Del: bad code    -- htt
ps://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     -- http
s://master.php.net/note/delete/78123/in+docs
Del: other reasons-- https://mast
er.php.net/note/delete/78123
Reject      -- https://mast
er.php.net/note/reject/78123
Search      -- https://
master.php.net/manage/user-notes.php

-- 
PHP Notes Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub
.php


[1]

about | contact  Other archives ( Real Estate discussion Medical topics )