用户登录
用户注册

分享至

PHP 的单例模式代码

  • 作者: 花花与美少年
  • 来源: 51数据库
  • 2022-08-23
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");

软件
前端设计
程序设计
Java相关