namespace root;
class Factor {
protected static $instance = null;
private function __construct() {
}
public static function getInstance() {
if (!self::$instance) {
$name = get_called_class();
self::$instance = new $name();
}
return self::$instance;
}
}
namespace admin\test;
use root\Factor;
class Single extends Factor {
public function abc() {
return 'abc';
}
}
namespace index;
use admin\test\Single;
class Index {
public function get() {
return Single::getInstance();
}
}
$index = new Index();
var_dump($index->get());
The result is:
object(admin\test\Single)#2 (0) {
}