<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/reserved.interfaces.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'zh',
  ),
  'this' => 
  array (
    0 => 'class.weakreference.php',
    1 => 'WeakReference',
    2 => 'WeakReference 类',
  ),
  'up' => 
  array (
    0 => 'reserved.interfaces.php',
    1 => '预定义接口和类',
  ),
  'prev' => 
  array (
    0 => 'fiber.getcurrent.php',
    1 => 'Fiber::getCurrent',
  ),
  'next' => 
  array (
    0 => 'weakreference.construct.php',
    1 => 'WeakReference::__construct',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'zh',
    'path' => 'language/predefined/weakreference.xml',
  ),
  'history' => 
  array (
  ),
  'extra_header_links' => 
  array (
    'rel' => 'alternate',
    'href' => '/manual/en/feeds/class.weakreference.atom',
    'type' => 'application/atom+xml',
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="class.weakreference" class="reference">

 <h1 class="title">WeakReference 类</h1>
 

 <div class="partintro"><p class="verinfo">(PHP 7 &gt;= 7.4.0, PHP 8)</p>


  <div class="section" id="weakreference.intro">
   <h2 class="title">简介</h2>
   <p class="para">
    弱引用允许保留对对象的引用，而不会阻止销毁对象。对于实现类似缓存的结构很有用。如果原始对象已销毁，则调用
    <span class="methodname"><a href="weakreference.get.php" class="methodname">WeakReference::get()</a></span> 方法时将返回 <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong>。当原始对象的 <a href="features.gc.refcounting-basics.php" class="link">refcount</a> 降至零时，会销毁原始对象；创建弱引用不会增加被引用对象的
    <code class="literal">refcount</code>。
   </p>
   <p class="para">
    <span class="classname"><strong class="classname">弱引用类</strong></span>不能序列化。
   </p>
  </div>


  <div class="section" id="weakreference.synopsis">
   <h2 class="title">类摘要</h2>


   <div class="classsynopsis"><div class="classsynopsisinfo">
    
     <span class="modifier">final</span>
     <span class="modifier">class</span> <strong class="classname"><strong class="classname">WeakReference</strong></strong>
     {</div>
    
    <div class="classsynopsisinfo classsynopsisinfo_comment">/* 方法 */</div>
    <div class="constructorsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><a href="weakreference.construct.php" class="methodname">__construct</a></span>()</div>

    <div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="modifier">static</span> <span class="methodname"><a href="weakreference.create.php" class="methodname">create</a></span>(<span class="methodparam"><span class="type"><a href="language.types.object.php" class="type object">object</a></span> <code class="parameter">$object</code></span>): <span class="type"><a href="class.weakreference.php" class="type WeakReference">WeakReference</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="weakreference.get.php" class="methodname">get</a></span>(): <span class="type"><span class="type"><a href="language.types.null.php" class="type null">?</a></span><span class="type"><a href="language.types.object.php" class="type object">object</a></span></span></div>

   }</div>


  </div>

  <div class="section" id="weakreference.examples">
   <h2 class="title">弱引用示例</h2>
   <p class="para">
    <div class="example" id="weakreference.basic-example">
     <p><strong>示例 #1 弱引用的基础用法</strong></p>
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br />$obj </span><span style="color: #007700">= new </span><span style="color: #0000BB">stdClass</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$weakref </span><span style="color: #007700">= </span><span style="color: #0000BB">WeakReference</span><span style="color: #007700">::</span><span style="color: #0000BB">create</span><span style="color: #007700">(</span><span style="color: #0000BB">$obj</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$weakref</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">get</span><span style="color: #007700">());<br /><br />unset(</span><span style="color: #0000BB">$obj</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$weakref</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">get</span><span style="color: #007700">());<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
     </div>

     <div class="example-contents"><p>以上示例的输出类似于：</p></div>
     <div class="example-contents screen">
<div class="cdata"><pre>
object(stdClass)#1 (0) {
}
NULL
</pre></div>
     </div>
    </div>
   </p>
  </div>

  <div class="section">
   <h2 class="title">更新日志</h2>
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>8.4.0</td>
       <td>
        现在 <span class="methodname"><strong>WeakReference::__debugInfo()</strong></span>
        的输出包含引用的对象，当引用不可用时，为 <code class="literal">NULL</code>。
       </td>
      </tr>

     </tbody>
    
   </table>

  </div>

 </div>

 





 






 






<h2>目录</h2><ul class="chunklist chunklist_reference"><li><a href="weakreference.construct.php">WeakReference::__construct</a> — 不允许实例化的构造函数</li><li><a href="weakreference.create.php">WeakReference::create</a> — 创建新的弱引用</li><li><a href="weakreference.get.php">WeakReference::get</a> — 获取弱引用对象</li></ul>
</div>
<?php manual_footer($setup); ?>