💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
# tp5 单例模式 ## 单例模式 ### 需要三个条件 1. 拥有一个构造函数 并且为private 2. 拥有一个静态成员变量用来保持类的实例 3. 拥有一个访问这个实例的静态方法 ### 为了简化操作 在extend目录下面建立一个Single.php ``` <pre class="calibre13">``` <span class="token"><</span><span class="token">?</span>php <span class="token2">class</span> <span class="token3">Single</span> <span class="token1">{</span> <span class="token6">/** 单例模式 * @var 单例模式需要三个条件 * 1. 拥有一个构造函数 并且为private * 2. 拥有一个静态成员变量来保持类的实例 * 3. 拥有一个访问这个实例的静态方法 */</span> <span class="token6">// 条件二: 拥有一个静态成员变量来保持类的实例</span> <span class="token2">static</span> <span class="token2">public</span> $instance <span class="token">=</span> <span class="token2">null</span><span class="token1">;</span> <span class="token6">// 条件一:拥有一个构造函数 并且为private 为了防止外部调用new来创建对象</span> <span class="token2">private</span> <span class="token2">function</span> <span class="token3">__construct</span><span class="token1">(</span><span class="token1">)</span><span class="token1">{</span> echo <span class="token4">'new -class<br>'</span><span class="token1">;</span> <span class="token1">}</span> <span class="token6">// 条件三:拥有一个访问这个实例的静态方法</span> <span class="token2">public</span> <span class="token2">static</span> <span class="token2">function</span> <span class="token3">getInstance</span><span class="token1">(</span><span class="token1">)</span><span class="token1">{</span> <span class="token2">if</span> <span class="token1">(</span><span class="token">!</span>self<span class="token1">:</span><span class="token1">:</span>$instance<span class="token1">)</span><span class="token1">{</span> self<span class="token1">:</span><span class="token1">:</span>$instance <span class="token">=</span> <span class="token2">new</span> <span class="token3">self</span><span class="token1">(</span><span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span> <span class="token2">return</span> self<span class="token1">:</span><span class="token1">:</span>$instance<span class="token1">;</span> <span class="token1">}</span> <span class="token2">public</span> <span class="token2">function</span> <span class="token3">getAbc</span><span class="token1">(</span><span class="token1">)</span><span class="token1">{</span> <span class="token2">return</span> <span class="token4">'abc'</span><span class="token1">;</span> <span class="token1">}</span> <span class="token1">}</span> ``` ``` 在首页进行调用 ``` <pre class="calibre14">``` <span class="token"><</span><span class="token">?</span>php namespace app\index\controller<span class="token1">;</span> use Single<span class="token1">;</span> <span class="token2">class</span> <span class="token3">Index</span> <span class="token1">{</span> <span class="token2">public</span> <span class="token2">function</span> <span class="token3">index</span><span class="token1">(</span><span class="token1">)</span> <span class="token1">{</span> <span class="token6">// 写法一</span> <span class="token6">// $a = \Single::getInstance()->getAbc();</span> <span class="token6">// \Single::getInstance()->getAbc();</span> <span class="token6">// \Single::getInstance()->getAbc();</span> <span class="token6">// \Single::getInstance()->getAbc();</span> <span class="token6">// \Single::getInstance()->getAbc();</span> <span class="token6">// 写法二 因为是把类下载了extend目录 所以可以在上部 使用use single;</span> $a <span class="token">=</span> Single<span class="token1">:</span><span class="token1">:</span><span class="token3">getInstance</span><span class="token1">(</span><span class="token1">)</span><span class="token">-</span><span class="token">></span><span class="token3">getAbc</span><span class="token1">(</span><span class="token1">)</span><span class="token1">;</span> echo $a<span class="token1">;</span> <span class="token1">}</span> <span class="token6">/**浏览器输出 new-class abc * * 只返回了一次 new-class 意思就是不管调用了几次 其实只调用了一次new class */</span> <span class="token1">}</span> ``` ```