note 52513 added to language.oop5.basic

From: Date: Wed, 04 May 2005 09:21:51 +0000
Subject: note 52513 added to language.oop5.basic
Groups: php.notes 
Request: Send a blank email to php-notes+get-88899@lists.php.net to get a copy of this message
Not that it is not possible to access a with 'new' newly constructed object without assigning it to a variable. This works: <?php class Foo { function __construct() { } function printHello() { echo "Hello!\n"; } } class Working { function __construct() { } function returnObject() { return new Foo(); } } $working = new Working(); $working->returnObject()->printHello(); ?> In the above example, the method returnObject() constructs a new Foo()-instance and returns it. Because PHP5 is capable to dereference it, I can use multiple method-calls in one line. Now to the point: This works _not_: <?php class Foo { function __construct() { } function printHello() { echo "Hello!\n"; } } class Working { function __construct() { } function returnObject() { return new Foo(); } } new Working()->returnObject()->printHello(); ?> Mainly the same code - I just did not assign the newly constructed Working-instance to a variable. This will break with an error. ---- Manual Page -- http://www.php.net/manual/en/language.oop5.basic.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+52513 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+52513&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+52513&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+52513&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+52513&report=yes&reason=useless Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+52513&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+52513&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#88899) next »