Answer the question
In order to leave comments, you need to log in
Singleton question?
I have already read about him more than once, incl. See all answers in a recent thread What is a singleton for?
Take a simple implementation without a wrapper:
$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
try {
$pdo = new PDO($dsn, $user, $pass, $opt);
} catch...
// Далее в приложении используем
$db=$pdo->prepare(" SELECT ");
class Singleton
{
protected static $instance;
public static function getInstance()
{
if (null === static::$instance) {
static::$instance = new static();
}
return static::$instance;
}
protected function __construct(){}
private function __clone(){}
private function __wakeup(){}
}
class SingletonChild extends Singleton{}
$obj = Singleton::getInstance();
\var_dump($obj === Singleton::getInstance()); // bool(true)
$anotherObj = SingletonChild::getInstance();
\var_dump($anotherObj === Singleton::getInstance()); // bool(false)
\var_dump($anotherObj === SingletonChild::getInstance()); // bool(true)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question