PHP singleton
普通にやるなら
class MyClass { /** * このクラスの単一のインスタンスを返却します。 */ function &getInstance() { static $singleton; if (!isset($singleton)) { $singleton = new MyClass; } return $singleton; } } $instance =& MyClass::getInstance();
以下では$GLOBALSを使ってクラスに静的変数を実現する方法も紹介されている。
- phpPatterns() - Singleton Pattern
http://www.phppatterns.com/index.php/article/articleview/6/1/1/
WebKreator - PHP Static Class Variables
http://www.webkreator.com/php/techniques/php-static-class-variables.html