array ( 0 => 'index.php', 1 => 'PHP Manual', ), 'head' => array ( 0 => 'UTF-8', 1 => 'zh', ), 'this' => array ( 0 => 'class.weakreference.php', 1 => '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', ), '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); ?>
(PHP 7 >= 7.4.0, PHP 8)
弱引用可以指向一个对象,并且不阻止对象的销毁。可以实现具有对象结构的缓存。
弱引用类不能序列化。
示例 #1 弱引用的基础用法
<?php
$obj = new stdClass;
$weakref = WeakReference::create($obj);
var_dump($weakref->get());
unset($obj);
var_dump($weakref->get());
?>
以上示例的输出类似于:
object(stdClass)#1 (0) { } NULL