Re: function call chaining
| From: | Gwynne Raskind | Date: | Wed, 20 Jan 2010 02:28:40 +0000 |
| Subject: | Re: function call chaining | ||
| References: | 1 2 3 4 5 6 7 8 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-46831@lists.php.net to get a copy of this message | ||
On Jan 19, 2010, at 5:54 PM, Alain Williams wrote:
>>> $eep->oop()->ork()->ah()->ah();
>>>
>>> the newcomer will have to spend significant time rummaging around the
>>> source code to figure out what classes are involved.
>> As opposed to:
>> $oop = $eep->oop();
>> $ork = $oop->ork();
>> $ah = $ork->ah();
>> $ah2 = $ah->ah();
>>
>> where it instantly becomes crystal clear! Come on, this argument of "I
>> could read any code without ever knowing anything about anything but you
>> feature broke it" is getting really stale. You couldn't, and the feature
>> changed nothing.
> +1
>
> At least with $eep->oop()->ork()->ah()->ah() you don't have stray/unwanted
> variables hanging round to confuse (or be misused) later.
+1
It also supports the "allocation is not initialization" (or anti-RAII) pattern found in
languages like Objective-C:
Objective-C:
String *s = [[String alloc] initWithFormat:@"I'm a little teapot, %@ and %@",
@"short", @"stout"];
PHP:
$s = (new String)->sprintf("I'm a little teapot, %s and %s", "short",
"stout");
(Obviously a very, very contrived example, meant only to illustrate the pattern.)
-- Gwynne