|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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
---
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Oct 25 10:00:01 2025 UTC |
Closing as this is possible as oh PHP 5.4: (new location)->city('Boston')->state('Massachusetts')->getMap();