Re: late static binding php6

From: Date: Thu, 22 Nov 2007 20:29:19 +0000
Subject: Re: late static binding php6
References: 1 2 3 4 5 6 7 8 9 10  Groups: php.internals 
Request: Send a blank email to internals+get-33378@lists.php.net to get a copy of this message
Hi List, just to drop my note here, i asked (i think) 2 years ago for such a feature to automate my singleton pattern. Not with late static bindings this is possible. <?php class singleton { static protected $_instance = null; static public function getInstance() { $caller = get_called_class(); if (!static::$_instance instanceof $caller) { static::$_instance = new $caller; } return static::$_instance; } } class foo extends singleton { } var_dump(foo::getInstance()); var_dump(foo::getInstance()); ?> i think this will also drop much redundant code from some frameworks. :) So this is one of my examples that helps much. -- Marco Kaiser

« previous php.internals (#33378) next »