On Wed, Jan 20, 2010 at 01:05:14PM +0100, Christian Grobmeier wrote:
> On Wed, Jan 20, 2010 at 11:35 AM, Christian Grobmeier
> <grobme...@gmail.com> wrote:
> >>> 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.
> 
>  I also think that:
>      $foo = (new bar())->someSetter();
>  is assigning the return value of the setter to $foo. I would love to
>  have a language feature like anonymous classes, but if $foo contains
>  the bar-object after this line - wow, how would I hate this. From my
>  understanding both examples should act differently.

someSetter() could return $this, although unlikely. The result of the line
above would be that the bar object is garbage collected after being created
& method someSetter() invoked. To keep it one would have to do:

    $foo = ($bar_obj = new bar())->someSetter();

I don't know if it would be useful in a 'new', but if you had a method
to find something (returning an objet) and then whole set of methods
on the returned object, eg:

    $johnSmith = $employees->findEmployee('John Smith');
    $johnSmith->increaseSalary(1000);

this could be simplified to:

    $employees->findEmployee('John Smith')->increaseSalary(1000);

This is the sort of reason why chaining is useful. It is also, IMHO,
quite readable.

Regards

-- 
Alain Williams
Linux/GNU Consultant - Mail systems, Web sites, Networking, Programmer, IT 
Lecturer.
+44 (0) 787 668 0256  http://www.phcomp.co.uk/
Parliament Hill Computers Ltd. Registration Information: 
http://www.phcomp.co.uk/contact.php
Past chairman of UKUUG: http://www.ukuug.org/
#include <std_disclaimer.h>

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

Reply via email to