龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > php编程 >

PHP 的单例模式

时间:2014-07-22 14:51来源: 作者: 点击:
分享到:
<无详细内容>
 class User {
    static function getInstance()
    {
    if (self::$instance == NULL) { // If instance is not created yet, will create it.
        self::$instance = new User();
    }
    return self::$instance;
    }
    private function __construct() 
    // Constructor method as private  so by mistake developer not crate
    // second object  of the User class with the use of new operator
    {
    }
    private function __clone()
    // Clone method as private so by mistake developer not crate 
    //second object  of the User class with the use of clone.
    {
    }
    
    function Log($str)
    { 
    echo $str;
    }
    static private $instance = NULL;
}
User::getInstance()->Log("Welcome User");
精彩图集

赞助商链接