我們最近有一個討論,如果有可能建立一個trait Singleton
PHP Traits,我們發揮它圍繞一個可能實現,但遇到了問題,與建築之一。建立一個Singleton特質與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
的問題是:它可以創建一個Singleton性狀的PHP?我們發現
我敢打賭,downvote/s是/是僅僅因爲你用這個詞「單身」。 –