To create a singleton, make the constructor private, disable cloning, disable extension and create a static variable to house the instance
final class President {
private static $instance;
private function __construct() {
// Hide the constructor
}
public static function getInstance() : President {
if (!self::$instance) {
self::$instance = new self();
}
return self::$instance;
}
private function __clone() {
// Disable cloning
}
private function __wakeup() {
// Disable unserialize
}
}
Then in order to use
$president1 = President::getInstance();
$president2 = President::getInstance();
var_dump($president1 === $president2); // true