Re: Re: PHP6 return by reference deprecation

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Sat, 2009-05-02 at 15:43 -0500, Larry Garfield wrote:
> On Saturday 02 May 2009 3:20:24 pm Colin Guthrie wrote:
> > 'Twas brillig, and Larry Garfield at 02/05/09 20:00 did gyre and gimble:
> > > On Saturday 02 May 2009 9:30:09 am Colin Guthrie wrote:
> > >> 'Twas brillig, and Paul M Foster at 02/05/09 06:07 did gyre and gimble:
> > >>> If this is going away, how do you return things by reference, so as to
> > >>> ensure a single copy of something (yes, I know the singleton pattern
> > >>> can be used; I do use it as well; it's more complicated)?
> > >>
> > >> You'll want to use the Singleton design pattern here.
> > >>
> > >> Let's say you're config object is a class.
> > >
> > > That's well and good if the thing you want a single copy of is an object.
> > >  The way objects pass in PHP 5 makes singletons easy.  But I actually
> > > just developed a system for PHP 5.2 that includes a class that
> > > deliberately allows a caller to reach in and grab an internal array-based
> > > data structure for special cases.
> > >
> > > class Foo {
> > >   protected $internalConfig = array(...);
> > >
> > >   public function &getConfig() {
> > >     return $this->internalConfig;
> > >   }
> > > }
> > >
> > > $foo = new Foo();
> > > ...
> > > $config = &$foo->getConfig();
> > > // Do stuff to $config that wouldn't make sense to do via methods.
> > >
> > > So do I understand the OP correctly that is going to break with PHP 6
> > > now?  I certainly hope not, as that would be incredibly short sighted and
> > > stupid. There are plenty of use cases for returning by reference besides
> > > making PHP 4 objects behave correctly.
> >
> > Use ArrayObject rather than just array. e.g.
> >
> > class Foo {
> >    protected $internalConfig;
> >
> >    public function __construct() {
> >      $this->internalConfig = new ArrayObject(...);
> >    }
> >
> >    public function getConfig() {
> >      return $this->internalConfig;
> >    }
> > }
> >
> > http://www.php.net/manual/en/class.arrayobject.php
> >
> > Col
> 
> If it were just a simple one level array, sure.  But it's not.  It's actually 
> quite deep and complex.  (That's why exposing it is a better plan than just 
> offering an accessor.)
> 
> ArrayAccess is also dramatically slower than regular arrays:
> http://www.garfieldtech.com/blog/magic-benchmarks
> 
> So forcing everything through an object is still pointless and a performance 
> loss.

I don't know if it's even the case that it's going to be removed. I just
went back and checked the link to the article that was posted and it was
written in 2005. many things have changed since then and the article
isn't very accurate anymore. For instance the goto operator was added to
PHP 5.3 (woot) yet the article indicates it was not going to be added in
PHP6. So obviously it's dated. I would check internals before getting
too concerned.

Cheers,
Rob.
-- 
http://www.interjinn.com
Application and Templating Framework for PHP


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux