- /* @package design pattern * @version v1.0 2011 - 12 - 14 */
- class Singleton {
- // 私有静态成员变量, 保存全局实例 private static $instance = NULL;
- // 私有构造方法, 保证外界无法直接实例化
- private
- function __construct() {}
- // 静态方法, 返回此类唯一实例
- public static
- function getInstance() {
- if (!isset(self: :$instance)) {
- $c = __CLASS__;
- self: :$instance = new $c;
- }
- return self: :$instance;
- }
- // 测试用方法
- public
- function info() {
- return 'ok';
- }
- // 防止克隆
- public
- function __clone() {
- trigger_error('Clone is not allowed.', E_USER_ERROR);
- }
- }*/
来源: https://www.php1.cn/detail/php-1bed48d721.html