array ( 0 => 'index.php', 1 => 'PHP Manual', ), 'head' => array ( 0 => 'UTF-8', 1 => 'ja', ), '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' => 'ja', '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)
弱い参照により、オブジェクトが破棄されるのを妨げないオブジェクトへの参照を保持することが可能です。 この機能は、キャッシュのようなデータ構造を実装するのに役立ちます。
WeakReference クラスはシリアライズできません。
例1 基本的な WeakReference クラスの使い方
<?php
$obj = new stdClass;
$weakref = WeakReference::create($obj);
var_dump($weakref->get());
unset($obj);
var_dump($weakref->get());
?>
上の例の出力は、 たとえば以下のようになります。
object(stdClass)#1 (0) { } NULL