合规国际互联网加速 OSASE为企业客户提供高速稳定SD-WAN国际加速解决方案。 广告
# Weakref扩展 弱引用允许程序员保留对对象的引用,而该对象不会阻止对象被销毁;它们对于实现类似缓存的结构非常有用。它们目前在PHP中得到了扩展支持。 在php7.4以前WeakRef实现都是通过重载对象处理程序来实现的,最流行的实现(pecl-weakref)改变了分配给只读内存的堆栈,PHP 7.3将使其不可用。以其他方式更改对象处理程序的其他实现也存在出现故障的风险,并且依赖于未定义的行为和抛弃一致性。 PHP 7.4 引入了WeakReference (弱引用)类型(也就是说php7.4无需此扩展了) 安装:<https://pecl.php.net/package/weakref>. Example#1 Weakref用法示例 ``` <pre class="calibre17">``` <span class="token1"><</span><span class="token1">?</span>php class <span class="token4">MyClass</span> <span class="token3">{</span> public <span class="token5">function</span> <span class="token4">__destruct</span><span class="token3">(</span><span class="token3">)</span> <span class="token3">{</span> echo <span class="token2">"Destroying object!\n"</span><span class="token3">;</span> <span class="token3">}</span> <span class="token3">}</span> $o1 <span class="token1">=</span> <span class="token5">new</span> <span class="token4">MyClass</span><span class="token3">;</span> $r1 <span class="token1">=</span> <span class="token5">new</span> <span class="token4">Weakref</span><span class="token3">(</span>$o1<span class="token3">)</span><span class="token3">;</span> <span class="token5">if</span> <span class="token3">(</span>$r1<span class="token1">-</span><span class="token1">></span><span class="token4">valid</span><span class="token3">(</span><span class="token3">)</span><span class="token3">)</span> <span class="token3">{</span> echo <span class="token2">"Object still exists!\n"</span><span class="token3">;</span> <span class="token4">var_dump</span><span class="token3">(</span>$r1<span class="token1">-</span><span class="token1">></span><span class="token4">get</span><span class="token3">(</span><span class="token3">)</span><span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span> <span class="token5">else</span> <span class="token3">{</span> echo <span class="token2">"Object is dead!\n"</span><span class="token3">;</span> <span class="token3">}</span> <span class="token4">unset</span><span class="token3">(</span>$o1<span class="token3">)</span><span class="token3">;</span> <span class="token5">if</span> <span class="token3">(</span>$r1<span class="token1">-</span><span class="token1">></span><span class="token4">valid</span><span class="token3">(</span><span class="token3">)</span><span class="token3">)</span> <span class="token3">{</span> echo <span class="token2">"Object still exists!\n"</span><span class="token3">;</span> <span class="token4">var_dump</span><span class="token3">(</span>$r1<span class="token1">-</span><span class="token1">></span><span class="token4">get</span><span class="token3">(</span><span class="token3">)</span><span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span> <span class="token5">else</span> <span class="token3">{</span> echo <span class="token2">"Object is dead!\n"</span><span class="token3">;</span> <span class="token3">}</span> <span class="token1">?</span><span class="token1">></span> 上面的示例将输出: Object still exists<span class="token1">!</span> <span class="token4">object</span><span class="token3">(</span>MyClass<span class="token3">)</span>#<span class="token6">1</span> <span class="token3">(</span><span class="token6">0</span><span class="token3">)</span> <span class="token3">{</span><span class="token3">}</span> Destroying object<span class="token1">!</span> Object is dead<span class="token1">!</span> ``` ```