单例模式笔记

<?php

class User {
//静态变量保存全局实例
private static $_instance = null;
//私有构造函数,防止外界实例化对象
private function __construct() {
}
//私有克隆函数,防止外办克隆对象
private function __clone() {
}
//静态方法,单例统一访问入口
static public function getInstance() {
if (is_null ( self::$_instance ) || isset ( self::$_instance )) {
self::$_instance = new self ();
}
return self::$_instance;
}
public function getName() {
echo 'hello world!';
}
}

?>

posted @ 2015-12-08 23:22  brady-wang  阅读(186)  评论(0编辑  收藏  举报