php设计模式之单例模式代码
php设计模式之单例模式的例子,供大家参考,具体内容如下 //protected getter for singleton instances
protected static function getSingleton($className){
//保证单例模式 并且不能从控制器实例化和克隆
if (!isset(self::$instanceMap[$className])) {
$object = new $className;
//Make sure this object inherit from Singleton
if ($object instanceof Fruit) {
self::$instanceMap[$className] = $object;
var_dump($object);
} else {
throw SingletonException("Class '$className' do not inherit from Singleton!");
}
}
return self::$instanceMap[$className];
}
//protected constructor to prevent outside instantiation
protected function __construct(){}
//denie cloning of singleton objects
public final function __clone(){
trigger_error('It is impossible to clone singleton',E_USER_ERROR);
}
} class Apple extends Fruit{
} class GreenApple extends Apple{
} $apple1 = Apple::getInstance(); 以上就是本文的全部内容,希望对大家学习php程序设计有所帮助。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |