Why does PHP create a new instance of a singleton each time the script calling Foo::getInstance() is run?
问题 I've suspected for while that PHP singletons are not real singletons, so I did a test. I created the following class: class MySingleton { private static $instance; private function __construct() { error_log("I am a new instance of MySingleton. I was instantiated at " . time()); } private function __clone(){} public static function getInstance() { if(!is_object(self::$instance)) { self::$instance = new MySingleton(); } return self::$instance; } } Next I created a file called test.php: require(