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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
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

Pull Requests

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: Sat Dec 21 17:01:58 2024 UTC