On Tue, Jan 19, 2010 at 12:02 PM, Rasmus Lerdorf <ras...@lerdorf.com> wrote:
> Eddie Drapkin wrote:
>> On Tue, Jan 19, 2010 at 11:05 AM, Stanislav Malyshev <s...@zend.com> wrote:
>>> The second was next on my list, while the first seems to me kind of exotic -
>>> why create object only to call one method and immediately drop it? Why this
>>> method is not static then?
>>
>>
>> Why would this imply "dropping" the object?
>>
>> This:
>>    $foo = (new bar())->someSetter();
>> Looks a lot better than this
>>    $foo = new bar();
>>    $foo->someSetter();
>
> The second version is much clearer.  You know exactly what $foo is.  In
> the shortened version you have no idea what $foo is without reading the
> code for the someSetter() method.  On first glance I would assume that
> $foo would be the success/failure return of the setter and that the
> object is dropped.
>
> -Rasmus
>

I don't think that's necessarily a fair statement to make.  If you
were working within a set of guidelines, or even within just a style,
that "dictated" that setters return $this consistently across several
class (or even all of them), it wouldn't be any less clear than the
second.   Assuming that you had no idea what the class did, or what
any of its methods returned, you could make the argument that the
former is less readable, but then again, if you're making wild guesses
at what methods return without reading code/documentation, you've got
bigger issues than the readability of a constructor + method call.

Even accepting the argument against readability, I don't think it's
strong enough to say that the feature is unacceptable.  If used
properly, it can very well aid in the concision and readability of
code (especially if you're a fan of the "fluent" interface); I don't
agree with the thinking that presenting a case where a feature can be
used improperly is grounds for its dismissal.  Were that the case,
nearly every feature in the language would be able to be dismissed,
no?

And to illustrate what I mean by fluent interface, there's a lot of
cases in code that I've written where I have something that looks
like:

Class::someFactory()->blah()->blah()->foo()->bar()->baz()->hello()->world();
//well, this is a little extreme

Which I think looks much better, and is more self documenting, as:

(new Class())->blah()->blah()->foo()->bar()->baz()->hello()->world();

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to