array ( 0 => 'index.php', 1 => 'PHP Manual', ), 'head' => array ( 0 => 'UTF-8', 1 => 'pt_BR', ), 'this' => array ( 0 => 'class.weakreference.php', 1 => 'WeakReference', ), 'up' => array ( 0 => 'reserved.interfaces.php', 1 => 'Interfaces e Classes pré-definidas', ), 'prev' => array ( 0 => 'fiber.getcurrent.php', 1 => 'Fiber::getCurrent', ), 'next' => array ( 0 => 'weakreference.construct.php', 1 => 'WeakReference::__construct', ), 'alternatives' => array ( ), 'source' => array ( 'lang' => 'pt_BR', '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)
Referências fracas permitem que o programador mantenha uma referência a um objeto que não impede que o objeto seja destruído. Eles são úteis para implementar estruturas como cache.
WeakReferences não podem ser serializadas.
Exemplo #1 Uso básico de referência fraca
<?php
$obj = new stdClass;
$weakref = WeakReference::create($obj);
var_dump($weakref->get());
unset($obj);
var_dump($weakref->get());
?>
O exemplo acima produzirá algo semelhante a:
object(stdClass)#1 (0) { } NULL