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.
--
Larry Garfield
[email protected]
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php