Re: PHP6 return by reference deprecation

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

 



'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

--

Colin Guthrie
gmane(at)colin.guthr.ie
http://colin.guthr.ie/

Day Job:
  Tribalogic Limited [http://www.tribalogic.net/]
Open Source:
  Mandriva Linux Contributor [http://www.mandriva.com/]
  PulseAudio Hacker [http://www.pulseaudio.org/]
  Trac Hacker [http://trac.edgewall.org/]


--
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