On Fri, Nov 26, 2010 at 9:25 PM, Peter Lind <peter.e.l...@gmail.com> wrote:
> On 26 November 2010 20:36, Felipe Pena <felipe...@gmail.com> wrote: > > Hi all, > > I'm here again to presents another proposal, which adds support for > > instantiating a class and calling its methods and accessing its > properties > > on same command. > > > > Example: > > > > <?php > > > > class bar { > > public $x = 'PHP'; > > } > > > > class foo extends bar { > > public function bar() { > > return $this; > > } > > } > > > > var_dump(new foo()->bar()->x); // string(3) "PHP" > > > > ?> > > > > Other examples which describes the feature at > > http://wiki.php.net/rfc/instance-method-call > > > > Thoughts? > > It seems fairly handy and I've been in situations where I wanted to do > something like that - in fact, I use factories to achieve something > similar. > However, the more I use it, the more it feels like introducing code > smells into my code. You're essentially instantiating an object only > to immediately throw it away. That means you don't actually need the > object at all, you should probably be looking for static methods or > class properties. Trying to avoid statics by introducing a way to > instantiate and throw away objects in the same statement feels a lot > like reinventing OOP while adding overhead. > > Anyway, just a personal observation. I generally favour the way that > PHP allows you to dig your own grave (i.e. I love the freedom of the > language), so as a developer I would probably favour this as well, > though I find it mainly a way to introduce hacks. > > 1, I have to use a non-trivial library or "module" for a simple task, and I don't want to write 20 line of code, and introduce 4 helper variable. 2. I want to get from point 1 to point 5 but I'm not interested in the steps in-between (classical method chaining), but sadly one of the steps requires object instantiation. Tyrael