note 52513 deleted from language.oop5.basic by bjori
| From: | bjori@php.net | Date: | Wed, 26 Apr 2006 16:49:15 +0000 |
| Subject: | note 52513 deleted from language.oop5.basic by bjori | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-109773@lists.php.net to get a copy of this message | ||
Note Submitter: tobias_demuth at web dot de
----
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.