下面为大家提供一个应用单例模式的小例子,供大家参考。
复制代码 代码如下:
<?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单例模式为何只能实例化一次
php设计模式之单例模式的实例代码
学习php设计模式之单例模式
php实现的单例模式的例子
学习php单例模式及应用实例
有关php单例模式介绍及例子
php设计模式之单例模式学习
php单例模式的例子