php.net |  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
Votes:2
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
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: michael dot patrick dot mcnally at gmail dot com
New email:
PHP Version: OS:

 

 [2009-12-10 19:56 UTC] 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
---


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-12-28 12:43 UTC] jani@php.net
-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] nikic@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: nikic
 [2012-04-10 23:13 UTC] nikic@php.net
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: Wed May 15 18:01:34 2024 UTC