Re: PHP6 return by reference deprecation
| From: | Colin Guthrie | Date: | Sat, 02 May 2009 14:30:09 +0000 |
| Subject: | Re: PHP6 return by reference deprecation | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-292216@lists.php.net to get a copy of this message | ||
'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/]