🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# 简单Contaniner容器 ``` <pre class="calibre15">``` <span class="token"><</span><span class="token">?</span>php <span class="token2">class</span> <span class="token3">Container</span> <span class="token1">{</span> <span class="token6">/** * @var 单例模式需要满足三个条件 1 拥有一个构造函数 并且为private * 2 拥有一个静态成员变量用来保持类的实例 * 3 拥有一个访问这个实例的静态方法 */</span> <span class="token6">/** * @var 注册模式是通过将对象实例注册到一个全局的对象树上,需要的时候从对象树上采摘下来使用 */</span> <span class="token6">// 存放容器的数据</span> <span class="token2">public</span> $instances <span class="token">=</span> <span class="token1">[</span><span class="token1">]</span><span class="token1">;</span> <span class="token6">// 容器的对象实例 拥有一个静态成员变量用来保持类的实例</span> <span class="token2">protected</span> <span class="token2">static</span> $instance<span class="token1">;</span> <span class="token6">// 构造函数并且为private 1 拥有一个静态构造函数 并且为private</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> <span class="token1">}</span> <span class="token6">//访问实例的静态方法 self 是区块类 static是当前类 拥有一个访问这个实例的静态方法</span> <span class="token2">public</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="token3">is_null</span><span class="token1">(</span><span class="token2">static</span><span class="token1">:</span><span class="token1">:</span>$instance<span class="token1">)</span><span class="token1">)</span> <span class="token1">{</span> <span class="token2">static</span><span class="token1">:</span><span class="token1">:</span>$instance <span class="token">=</span> <span class="token2">new</span> <span class="token3">static</span><span class="token1">(</span><span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span> <span class="token2">return</span> <span class="token2">static</span><span class="token1">:</span><span class="token1">:</span>$instance<span class="token1">;</span> <span class="token1">}</span> <span class="token6">// 把实例添加到注册树</span> <span class="token2">public</span> <span class="token2">function</span> <span class="token2">set</span><span class="token1">(</span>$key<span class="token1">,</span>$value<span class="token1">)</span> <span class="token1">{</span> $<span class="token2">this</span><span class="token">-</span><span class="token">></span>instances<span class="token1">[</span>$key<span class="token1">]</span><span class="token1">;</span> <span class="token1">}</span> <span class="token6">//从注册树获取实例</span> <span class="token2">public</span> <span class="token2">function</span> <span class="token2">get</span><span class="token1">(</span>$key<span class="token1">)</span> <span class="token1">{</span> $<span class="token2">new</span> <span class="token">=</span> $<span class="token2">this</span><span class="token">-</span><span class="token">></span>instances<span class="token1">[</span>$key<span class="token1">]</span><span class="token1">;</span> <span class="token2">return</span> $<span class="token2">new</span><span class="token1">;</span> <span class="token1">}</span> <span class="token1">}</span> ``` ```