温故而知新。下面的代码能否进一步升级的更完善?欢迎思考留言。
<?php
class Database
{
// 保存实例化对象
private static $instance;
//保存数据库句柄
private $db;
private function __construct(array $options)
{
// $this->db = mysqli_connect($options['dh_host'], $options['db_user'], $options['db_password'],
// $options['db_database']);
}
public static function getInstance(array $options): self
{
if (static::$instance === null) {
static::$instance = new static($options);
}
return static::$instance;
}
public function getDb()
{
return $this->db;
}
private function __clone()
{
// echo 'Clone' . PHP_EOL;
}
public function __wakeup()
{
// echo 'wakeup';
}
}
$database = Database::getInstance([]);
// $databaseNew = clone $database;
// $database = serialize($database);
// $database = unserialize($database);
|