php设计模式-单例模式
<h1>PHP单例模式</h1><p><span style="font-size: 16px">定义:简单的说,<strong><span style="color: rgba(255, 102, 0, 1)">整个应用中只有一个实例对象的设计模式</span></strong>。<br></span></p>
<h2>1、单例模式的要点:</h2>
<ul>
<li><span style="font-size: 16px">构造函数需要标记为private(访问控制:防止外部代码使用new操作符创建对象),单例类不能在其他类中实例化,只能被其自身实例化;</span></li>
<li><span style="font-size: 16px">拥有一个保存类的实例的静态成员变量</span></li>
<li><span style="font-size: 16px">拥有一个访问这个实例的公共的静态方法(常用getInstance()方法进行实例化单例类,通过instanceof操作符可以检测到类是否已经被实例化)</span></li>
</ul>
<h3>简单的记为三私一公一关键:</h3>
<ul>
<li><span style="font-size: 16px"><strong><span style="color: rgba(255, 0, 0, 1)">私有静态属性</span></strong>(<span style="color: rgba(255, 102, 0, 1)">privite static $instance</span>),又来储存生成的唯一对象</span></li>
<li><span style="font-size: 16px"><strong><span style="color: rgba(255, 0, 0, 1)">私有构造函数</span></strong> (<span style="color: rgba(255, 102, 0, 1)">privite __contruct()</span>)</span></li>
<li><span style="font-size: 16px"><strong><span style="color: rgba(255, 0, 0, 1)">私有克隆函数</span></strong>(<span style="color: rgba(255, 102, 0, 1)">privite function __clone()</span>),防止克隆——clone</span></li>
<li><span style="font-size: 16px"><strong><span style="color: rgba(255, 0, 0, 1)">公共静态方法</span></strong>(<span style="color: rgba(255, 102, 0, 1)">public static function getInstance()</span>),用来访问静态属性储存的对象,如果没有对象,则生成此单例</span></li>
<li><span style="font-size: 16px"><strong><span style="color: rgba(255, 0, 0, 1)">关键词instanceof</span></strong>,检查此变量是否为该类的对象、子类、或是实现接口。</span></li>
</ul>
<h2>2、为什么要使用PHP单例模式?</h2>
<ul>
<li><span style="font-size: 16px">php的应用主要在于数据库应用, 所以一个应用中会存在大量的数据库操作,<strong><span style="color: rgba(255, 102, 0, 1)"> 使用单例模式, 则可以避免大量的new 操作消耗的资源。</span></strong></span></li>
<li><span style="font-size: 16px">如果系统中需要有一个类来全局控制某些配置信息, 那么使用单例模式可以很方便的实现。</span></li>
<li><span style="font-size: 16px">在一次页面请求中, 便于进行调试, 因为所有的代码(例如数据库操作类db)都集中在一个类中, 我们可以<strong>在类中设置钩子, 输出日志</strong>,从而避免到处var_dump, echo。</span></li>
</ul>
<h2>3、单例模式解决的问题</h2>
<p><span style="font-size: 16px">单例模式解决的是<strong><span style="color: rgba(255, 102, 0, 1)">如何在整个项目中创建唯一对象实例</span></strong>的问题,单例模式和工厂模式可以产生更加合理的对象。</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
* @purpose: 创建一个单例类
* Class Single
</span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> Single {
</span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
* @var Object 保存类实例的静态成员变量
</span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(128, 0, 128, 1)">$_instance</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
* Single constructor. 私有的构造方法
</span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> __construct(){
</span><span style="color: rgba(0, 0, 255, 1)">echo</span> 'This is a Constructed method;'<span style="color: rgba(0, 0, 0, 1)">;
}
</span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
* @purpose: 创建__clone方法防止对象被复制克隆
</span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> __clone(){
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">E_USER_ERROR只能通过trigger_error($msg, E_USER_ERROR)手动触发。E_USER_ERROR是用户自定义错误类型,可以被set_error_handler错误处理函数捕获,允许程序继续运行。E_ERROR是系统错误,不能被set_error_handler错误处理函数捕获,程序会退出运行</span>
<span style="color: rgba(0, 128, 128, 1)">trigger_error</span>('Clone is not allow!',<span style="color: rgba(255, 0, 255, 1)">E_USER_ERROR</span><span style="color: rgba(0, 0, 0, 1)">);
}
</span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
* @return Single|Object 单例方法,用于访问实例的公共的静态方法
</span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> getInstance(){
</span><span style="color: rgba(0, 0, 255, 1)">if</span>(!(self::<span style="color: rgba(128, 0, 128, 1)">$_instance</span><span style="color: rgba(0, 0, 0, 1)"> instanceof self)){
self</span>::<span style="color: rgba(128, 0, 128, 1)">$_instance</span> = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> self;
}
</span><span style="color: rgba(0, 0, 255, 1)">return</span> self::<span style="color: rgba(128, 0, 128, 1)">$_instance</span><span style="color: rgba(0, 0, 0, 1)">;
}
</span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
* @purpose: 测试方法
</span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> test(){
</span><span style="color: rgba(0, 0, 255, 1)">echo</span> '调用方法成功'<span style="color: rgba(0, 0, 0, 1)">;
}
}</span></pre>
</div>
<p> </p>
<p> </p>
<p> </p>
<p>本文参考自博友的博客,感谢博友的分享:<strong><span style="color: rgba(0, 0, 255, 1)"><span style="color: rgba(0, 0, 255, 1)">https://www.cnblogs.com/zhhtao/p/4821140.html</span></span></strong></p><br><br>
来源:https://www.cnblogs.com/chrdai/p/11182665.html
頁:
[1]