首页 > 学院 > 开发设计 > 正文

单例模式

2019-11-08 01:26:31
字体:
来源:转载
供稿:网友
<?php// 单例模式class Singleton{	PRotected static $ins = null;	/**	 * 禁止子类重载 __construct() 构造方法	 */	private final function __construct() {		// 禁止 new	}	/**	 * 禁止子类重载 __clone() 方法	 */	protected final function __clone() {		// 禁止 clone	}	/*	public static function getIns() {		if(self::$ins === null){			self::$ins = new self();		}		return self::$ins;	}	*/	/**	 * 后期静态绑定	 */	public static function getIns() {		if(static::$ins === null){			static::$ins = new static();		}		return static::$ins;	}}$obj1 = Singleton::getIns();$obj2 = Singleton::getIns();var_dump($obj1 === $obj2); //true// $obj3 = clone $obj1; //不能被克隆
上一篇:final关键字

下一篇:leetcode513

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表