Re: Re: function call chaining

From: Date: Tue, 19 Jan 2010 17:24:10 +0000
Subject: Re: Re: function call chaining
References: 1 2 3 4 5 6  Groups: php.internals 
Request: Send a blank email to internals+get-46813@lists.php.net to get a copy of this message
Eddie Drapkin wrote: > On Tue, Jan 19, 2010 at 12:02 PM, Rasmus Lerdorf <rasmus@lerdorf.com> wrote: >> Eddie Drapkin wrote: >>> On Tue, Jan 19, 2010 at 11:05 AM, Stanislav Malyshev <stas@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. Wild guesses? The point is that it doesn't matter what the setter returns in the longer version because we know for a fact that the return is dropped whereas it matters a lot in the shorter version and that is where most people will guess wrong. And guess what, most people don't read documentation. Trust me on this one. -Rasmus

« previous php.internals (#46813) next »