<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Introduction</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="book.weakref.html">Weakref</a></div> <div class="next" style="text-align: right; float: right;"><a href="weakref.setup.html">Installing/Configuring</a></div> <div class="up"><a href="book.weakref.html">Weakref</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="intro.weakref" class="preface"> <h1 class="title">Introduction</h1> <p class="para"> Weak references provide a non-intrusive gateway to ephemeral objects. Unlike normal (strong) references, weak references do not prevent the garbage collector from freeing that object. For this reason, an object may be destroyed even though a weak reference to that object still exists. In such conditions, the weak reference seamlessly becomes invalid. </p> <p class="para"> <div class="example" id="example-547"> <p><strong>Example #1 <a href="class.weakref.html" class="classname">Weakref</a> usage example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #007700">class </span><span style="color: #0000BB">MyClass </span><span style="color: #007700">{<br /> public function </span><span style="color: #0000BB">__destruct</span><span style="color: #007700">() {<br /> echo </span><span style="color: #DD0000">"Destroying object!\n"</span><span style="color: #007700">;<br /> }<br />}<br /><br /></span><span style="color: #0000BB">$o1 </span><span style="color: #007700">= new </span><span style="color: #0000BB">MyClass</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$r1 </span><span style="color: #007700">= new </span><span style="color: #0000BB">Weakref</span><span style="color: #007700">(</span><span style="color: #0000BB">$o1</span><span style="color: #007700">);<br /><br />if (</span><span style="color: #0000BB">$r1</span><span style="color: #007700">-></span><span style="color: #0000BB">valid</span><span style="color: #007700">()) {<br /> echo </span><span style="color: #DD0000">"Object still exists!\n"</span><span style="color: #007700">;<br /> </span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$r1</span><span style="color: #007700">-></span><span style="color: #0000BB">get</span><span style="color: #007700">());<br />} else {<br /> echo </span><span style="color: #DD0000">"Object is dead!\n"</span><span style="color: #007700">;<br />}<br /><br />unset(</span><span style="color: #0000BB">$o1</span><span style="color: #007700">);<br /><br />if (</span><span style="color: #0000BB">$r1</span><span style="color: #007700">-></span><span style="color: #0000BB">valid</span><span style="color: #007700">()) {<br /> echo </span><span style="color: #DD0000">"Object still exists!\n"</span><span style="color: #007700">;<br /> </span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$r1</span><span style="color: #007700">-></span><span style="color: #0000BB">get</span><span style="color: #007700">());<br />} else {<br /> echo </span><span style="color: #DD0000">"Object is dead!\n"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> Object still exists! object(MyClass)#1 (0) { } Destroying object! Object is dead! </pre></div> </div> </div> </p> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="book.weakref.html">Weakref</a></div> <div class="next" style="text-align: right; float: right;"><a href="weakref.setup.html">Installing/Configuring</a></div> <div class="up"><a href="book.weakref.html">Weakref</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>