Re: new feature -> with()

From: Date: Fri, 12 Oct 2007 20:28:02 +0000
Subject: Re: new feature -> with()
References: 1  Groups: php.internals 
Request: Send a blank email to internals+get-32740@lists.php.net to get a copy of this message
hi, wow, thats what you're writing is a chain call. this only works, if the methods of a class return the object $this. but if you need a return value other than $this, simply there is no real way. and moreover i don't agree with the other arguments. what i'm suggesting is, that in with(){} you are only allowed to use the methods and variables defined in the object you are using with(). as in the sample, you are not using any global functions or else. my opinion is, this it is especially with $this very useful. greetings ----- Original Message ----- From: "Ralph Schindler" <ralph@smashlabs.com> To: "Sebastian" <sebastian@ifyouwantblood.de> Cc: <internals@lists.php.net> Sent: Wednesday, October 10, 2007 6:08 PM Subject: Re: [PHP-DEV] new feature -> with() > >> --------------------------- >> $class=new class; >> >> with($class) >> { >> do_something(); >> do_more(); >> do(); >> } > > What more value does this hold over implementing fluent interfaces which > are already possible? > > Assuming > class Foo { > public function doSomething() { /**code**/ return $this; } > public function doMore() { /**code**/ return $this; } > public function doDo() { /**code**/ return $this; } > } > > You can do this: > > $bar = new Foo() > $bar->doSomething()->doMore()->doDo(); > > or, if you want exceptional fallthrough: > > try { > $bar->doSomething() > ->doMore() > ->doDo(); > } catch (Exception $e) { > ... > } > > -ralph

« previous php.internals (#32740) next »