Re: PHP6 return by reference deprecation

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

 



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

Something like this should work very nicely.


class My_Config
{
  private function __construct()
  {
    // Whatever you need to do to load the config
  }


  public static function getInstance()
  {
    static $instance = null;
    if (null === $instance)
      $instance = new self();
    return $instance;
  }
}



Then in your code whenever you want to get your config object you would call:

$config = My_Config::getInstance();


You'll only every get one object created due to the private constructor and which enforces the use of the getInstance() method.

This is a very common design pattern, and is ideally suited to this use case.

HTHs

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