Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>No, it is not.</p> <ul> <li><p>Objects are always automatically passed as references (<strong>info!</strong> see below for additional important information!)</p></li> <li><p>Declaring paramters as references must happen at the function definition:</p> <pre><code>function test(&amp;$var) {} </code></pre> <p><strong>Not</strong> at the time of calling it:</p> <pre><code>// wrong $var = '123'; test(&amp;$var); </code></pre></li> </ul> <p><hr /> If you pass an object as usual, you can alter the object's state (properties). <br /> Assume that <code>$obj</code> is an instance of the class <code>TestClass</code> which contains a member variable called <code>hello</code> (<a href="http://ideone.com/4mzU3P" rel="nofollow">click here for a full sample at Ideone.com</a>):</p> <pre><code>function modify($obj) { $obj-&gt;hello = 'world (modified)!'; } $obj-&gt;hello = 'world'; modify($obj); var_dump($obj-&gt;hello); // outputs "world (modified!)" </code></pre> <p>This should be self-explanatory. Now, using the same code but assigning another value to <code>$obj</code> instead modifying the object's state results in no modification (<a href="http://ideone.com/KopB7N" rel="nofollow">&rarr; Ideone.com</a>):</p> <pre><code>function modify($obj) { $obj = 42; } // ... var_dump($obj-&gt;hello); // outputs "world" </code></pre> <p>Only accepting the parameter explicitly as a reference gives us the ability to completely change the variable's contents (<a href="http://ideone.com/XbFu7W" rel="nofollow">&rarr; Ideone.com</a>):</p> <pre><code>function modify(&amp;$obj) { $obj = 42; } // ... var_dump($obj); // outputs "42" </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload