Andrea Faulds wrote on 10/12/2014 17:19:
Finally, this may encourage bad code. As someone helpfully pointed out in the 
reddit discussion (http://redd.it/2ot15u), this encourages breaking the Law of 
Demeter, i.e. that objects should only talk to their immediate friends. 
Usually, long chains of method calls across multiple objects are not a good 
idea, yet the main benefit of this RFC is to reduce the boilerplate needed for 
that scenario.

It's an interesting thought, but reading the Wikipedia article, I see that that "Law" has acknowledged downsides, and so code breaking it is not universally acknowledged as "bad". I actually think it's a better argument against chaining property access (which implies deep knowledge of structures) than against chaining methods (which may be completely abstract actions unrelated to structure).

In particular, long method chains are commonly associated with "fluent interfaces", which can be a very effective way of creating a Domain-Specific Language in an OOP system - database query builders, for instance, tend to use this approach. It may not even involve exposing additional objects at all, just "return $this" at the end of a mutator.

On the other hand, methods designed to be used like that should have strong return contracts, and throw exceptions themselves on error, so neither engine exceptions nor an extra operator would be necessary if the underlying library was written well.

I'm kind of on the fence as to whether this is the right solution to the problem.

--
Rowan Collins
[IMSoP]

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

Reply via email to