<?php
// 设计模式之单例模式
class Teacher{
// 测试字段
public $test = 12;
//创建存放实例的空间
private static $ins;
// 私有构造函数
private function __construct(){
}
// 获取实例
public static function getInstance(){
if(!(self::$ins instanceof self)){
self::$ins = new self();
}
return self::$ins;
}
// 禁止克隆
private function __clone(){
}
}
// 创建实例
$teacher = Teacher::getInstance();
echo $teacher->test; //12
echo '<br>';
echo $teacher->test = 14; //14
// 再创建实例
$teacher = Teacher::getInstance();
echo '<br>';
echo $teacher->test; //14
echo '<br>';
?>