trait Singleton
PHP Traitsをビルドすることができたかどうかについて議論しましたが、可能なインプリメンテーションで試してみましたが、ビルド1で問題が発生しました。PHPでシングルトン特性を構築する5.4
これは学問的な課題です。は、私が知っているSingletons have very little - if not to say no - use in PHP
とone should 'just create one'
そのちょうど形質の可能性を探索するための:
<?php
trait Singleton
{
protected static $instance;
final public static function getInstance()
{
return isset(static::$instance)
? static::$instance
: static::$instance = new static;
}
final private function __construct() {
static::init();
}
protected function init() {}
final private function __wakeup() {}
final private function __clone() {}
}
class A {
use Singleton;
public function __construct() {
echo "Doesn't work out!";
}
}
$a = new A(); // Works fine
再生:http://codepad.viper-7.com/NmP0nZ
ある質問:PHPでのシングルトン形質を作成することは可能でしょうか?私たちが発見した
"シングルトン"。 –