单例模式

发布于:2020-02-27 08:26:52
<?php
class db
{
    static private $db;
    private $config;
    private function __construct($config)
    {
        $this->config = $config;
        $user = '';
        $pass = '';
        self::$db = new PDO('mysql:host=;dbname=;charset=utf8', $user, $pass);
    }

    static function getInstance($config)
    {
        if(!self::$db instanceof self){
            self::$db = new self($config);
        }
        return self::$db;
    }

    public function getName()
    {
        echo $this->config;
    }
}

$bb = db::getInstance(5);
$bb->getName();
// var_dump($bb);
$bb = db::getInstance(1);
$bb->getName();
阅读 122

匿名用户

结果输出是2个5

匿名用户

666666