<!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>Objects and references</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="language.oop5.late-static-bindings.html">Late Static Bindings</a></div> <div class="next" style="text-align: right; float: right;"><a href="language.oop5.serialization.html">Object Serialization</a></div> <div class="up"><a href="language.oop5.html">Classes and Objects</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="language.oop5.references" class="sect1"> <h2 class="title">Objects and references</h2> <p class="para"> One of the key-points of PHP 5 OOP that is often mentioned is that "objects are passed by references by default". This is not completely true. This section rectifies that general thought using some examples. </p> <p class="para"> A PHP reference is an alias, which allows two different variables to write to the same value. As of PHP 5, an object variable doesn't contain the object itself as value anymore. It only contains an object identifier which allows object accessors to find the actual object. When an object is sent by argument, returned or assigned to another variable, the different variables are not aliases: they hold a copy of the identifier, which points to the same object. </p> <div class="example" id="example-232"> <p><strong>Example #1 References and Objects</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">A </span><span style="color: #007700">{<br /> public </span><span style="color: #0000BB">$foo </span><span style="color: #007700">= </span><span style="color: #0000BB">1</span><span style="color: #007700">;<br />} <br /><br /></span><span style="color: #0000BB">$a </span><span style="color: #007700">= new </span><span style="color: #0000BB">A</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$b </span><span style="color: #007700">= </span><span style="color: #0000BB">$a</span><span style="color: #007700">; </span><span style="color: #FF8000">// $a and $b are copies of the same identifier<br /> // ($a) = ($b) = <id><br /></span><span style="color: #0000BB">$b</span><span style="color: #007700">-></span><span style="color: #0000BB">foo </span><span style="color: #007700">= </span><span style="color: #0000BB">2</span><span style="color: #007700">;<br />echo </span><span style="color: #0000BB">$a</span><span style="color: #007700">-></span><span style="color: #0000BB">foo</span><span style="color: #007700">.</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br /><br /></span><span style="color: #0000BB">$c </span><span style="color: #007700">= new </span><span style="color: #0000BB">A</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$d </span><span style="color: #007700">= &</span><span style="color: #0000BB">$c</span><span style="color: #007700">; </span><span style="color: #FF8000">// $c and $d are references<br /> // ($c,$d) = <id><br /><br /></span><span style="color: #0000BB">$d</span><span style="color: #007700">-></span><span style="color: #0000BB">foo </span><span style="color: #007700">= </span><span style="color: #0000BB">2</span><span style="color: #007700">;<br />echo </span><span style="color: #0000BB">$c</span><span style="color: #007700">-></span><span style="color: #0000BB">foo</span><span style="color: #007700">.</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br /><br /></span><span style="color: #0000BB">$e </span><span style="color: #007700">= new </span><span style="color: #0000BB">A</span><span style="color: #007700">;<br /><br />function </span><span style="color: #0000BB">foo</span><span style="color: #007700">(</span><span style="color: #0000BB">$obj</span><span style="color: #007700">) {<br /> </span><span style="color: #FF8000">// ($obj) = ($e) = <id><br /> </span><span style="color: #0000BB">$obj</span><span style="color: #007700">-></span><span style="color: #0000BB">foo </span><span style="color: #007700">= </span><span style="color: #0000BB">2</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">foo</span><span style="color: #007700">(</span><span style="color: #0000BB">$e</span><span style="color: #007700">);<br />echo </span><span style="color: #0000BB">$e</span><span style="color: #007700">-></span><span style="color: #0000BB">foo</span><span style="color: #007700">.</span><span style="color: #DD0000">"\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> 2 2 2 </pre></div> </div> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="language.oop5.late-static-bindings.html">Late Static Bindings</a></div> <div class="next" style="text-align: right; float: right;"><a href="language.oop5.serialization.html">Object Serialization</a></div> <div class="up"><a href="language.oop5.html">Classes and Objects</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>