Req #50442 [Opn->Csd]: __construct Method Chaining
| From: | nikic@php.net | Date: | Tue, 10 Apr 2012 23:13:57 +0000 |
| Subject: | Req #50442 [Opn->Csd]: __construct Method Chaining | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-169418@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=50442&edit=1
ID: 50442
Updated by: nikic@php.net
Reported by: michael dot patrick dot mcnally at gmail dot com
Summary: __construct Method Chaining
-Status: Open
+Status: Closed
Type: Feature/Change Request
Package: Class/Object related
Operating System: *
PHP Version: *
-Assigned To:
+Assigned To: nikic
Block user comment: N
Private report: N
New Comment:
Closing as this is possible as oh PHP 5.4:
(new location)->city('Boston')->state('Massachusetts')->getMap();
Previous Comments:
------------------------------------------------------------------------
[2009-12-10 19:56:23] michael dot patrick dot mcnally at gmail dot com
Description:
------------
Method chaining is very impressive when done properly. For example:
$location = new location();
$location->city('Boston')->state('Massachusetts')->getMap();
In JavaScript, it is possible to chain from the constructor, like this:
new location.city('Boston').state('Massachusetts').getMap();
Where in PHP, the equivalent would be:
new location()->city('Boston')->state('Massachusetts')->getMap();
Is there a reason why the __construct of a class cannot return itself for method chaining like
its' methods? When I try, I receive a Parse Error.
Thank you!
Reproduce code:
---------------
---
From manual page: language.oop5
---
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=50442&edit=1