设计模式 PHP 的单例模式
PHP 的单例模式 **[PHP]代码**```{.php} class User { static function getInstance() { if (self::$instance == NULL) { // If instance is not created yet, will create it. self::$instance = ne
[PHP]代码
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");
- 上一篇:php使用md5加密字符串
- 下一篇:php随机生成易于记忆的密码
精彩图集
精彩文章






