|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #50442 __construct Method Chaining
Submitted: 2009-12-10 19:56 UTC Modified: 2012-04-10 23:13 UTC
Avg. Score:5.0 ± 0.0
Reproduced:2 of 2 (100.0%)
Same Version:1 (50.0%)
Same OS:1 (50.0%)
From: michael dot patrick dot mcnally at gmail dot com Assigned: nikic (profile)
Status: Closed Package: Class/Object related
PHP Version: * OS: *
Private report: No CVE-ID: None
 [2009-12-10 19:56 UTC] michael dot patrick dot mcnally at gmail dot com
Method chaining is very impressive when done properly.  For example:

$location = new location();

In JavaScript, it is possible to chain from the constructor, like this:

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


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2010-12-28 12:43 UTC]
-Package: Feature/Change Request +Package: Class/Object related -Operating System: Windows Vista +Operating System: * -PHP Version: 5.2.11 +PHP Version: *
 [2012-04-10 23:13 UTC]
-Status: Open +Status: Closed -Assigned To: +Assigned To: nikic
 [2012-04-10 23:13 UTC]
Closing as this is possible as oh PHP 5.4:

(new location)->city('Boston')->state('Massachusetts')->getMap();
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Feb 29 13:01:32 2024 UTC