<?xml version="1.0" encoding="utf-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: PHP 5 Object References</title>
	<atom:link href="http://mjtsai.com/blog/2004/07/15/php-5-object-references/feed/" rel="self" type="application/rss+xml" />
	<link>http://mjtsai.com/blog/2004/07/15/php-5-object-references/</link>
	<description></description>
	<lastBuildDate>Sat, 20 Mar 2010 04:30:21 -0700</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Arvind</title>
		<link>http://mjtsai.com/blog/2004/07/15/php-5-object-references/comment-page-1/#comment-528452</link>
		<dc:creator>Arvind</dc:creator>
		<pubDate>Fri, 30 Oct 2009 02:09:18 +0000</pubDate>
		<guid isPermaLink="false">/?p=862#comment-528452</guid>
		<description>The difference can be pin pointed as follows:

&lt;pre&gt;
&lt;?php

class Label
{
&#160;&#160;&#160;&#160;var $text;
&#160;&#160;&#160;&#160;function setLabel($text)
&#160;&#160;&#160;&#160;{
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;$this-&gt;text = $text;
&#160;&#160;&#160;&#160;}
}

$foo = new Label();
$bar1 = $foo;
$bar2 =&amp; $foo;

$foo-&gt;setLabel(&quot;foo&quot;);


echo &quot;Original values: all are same\n&quot;;
echo &quot;foo:&quot;.$foo-&gt;text.&quot; bar1:&quot;.$bar1-&gt;text.&quot; bar2:&quot;.$bar2-&gt;text.&quot;\n&quot;;


$bar1=new Label();
$bar1-&gt;setLabel(&quot;bar1&quot;);

echo &quot;After bar1 was reassigned: notice that foo does not change its value:
foo and bar1 are not the same variable\n&quot;;
echo &quot;foo:&quot;.$foo-&gt;text.&quot; bar1:&quot;.$bar1-&gt;text.&quot; bar2:&quot;.$bar2-&gt;text.&quot;\n&quot;;

$bar2=new Label();
$bar2-&gt;setLabel(&quot;bar2&quot;);

echo &quot;After bar2 was reassigned: notice that foo changes its value: foo and
bar2 are the same variable\n&quot;;
echo &quot;foo:&quot;.$foo-&gt;text.&quot; bar1:&quot;.$bar1-&gt;text.&quot; bar2:&quot;.$bar2-&gt;text.&quot;\n&quot;;
?&gt;

PHP 5.2.9 (cli) (built: Apr 15 2009 09:30:33)
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2009 Zend Technologies

Original values: all are same
foo:foo bar1:foo bar2:foo
After bar1 was reassigned: notice that foo does not change its value: foo
and bar1 are not the same variable
foo:foo bar1:bar1 bar2:foo
After bar2 was reassigned: notice that foo changes its value: foo and bar2
are the same variable
foo:bar2 bar1:bar1 bar2:bar2
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>The difference can be pin pointed as follows:</p>
<pre>
&lt;?php

class Label
{
&nbsp;&nbsp;&nbsp;&nbsp;var $text;
&nbsp;&nbsp;&nbsp;&nbsp;function setLabel($text)
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;text = $text;
&nbsp;&nbsp;&nbsp;&nbsp;}
}

$foo = new Label();
$bar1 = $foo;
$bar2 =&amp; $foo;

$foo-&gt;setLabel("foo");

echo "Original values: all are same\n";
echo "foo:".$foo-&gt;text." bar1:".$bar1-&gt;text." bar2:".$bar2-&gt;text."\n";

$bar1=new Label();
$bar1-&gt;setLabel("bar1");

echo "After bar1 was reassigned: notice that foo does not change its value:
foo and bar1 are not the same variable\n";
echo "foo:".$foo-&gt;text." bar1:".$bar1-&gt;text." bar2:".$bar2-&gt;text."\n";

$bar2=new Label();
$bar2-&gt;setLabel("bar2");

echo "After bar2 was reassigned: notice that foo changes its value: foo and
bar2 are the same variable\n";
echo "foo:".$foo-&gt;text." bar1:".$bar1-&gt;text." bar2:".$bar2-&gt;text."\n";
?&gt;

PHP 5.2.9 (cli) (built: Apr 15 2009 09:30:33)
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2009 Zend Technologies

Original values: all are same
foo:foo bar1:foo bar2:foo
After bar1 was reassigned: notice that foo does not change its value: foo
and bar1 are not the same variable
foo:foo bar1:bar1 bar2:foo
After bar2 was reassigned: notice that foo changes its value: foo and bar2
are the same variable
foo:bar2 bar1:bar1 bar2:bar2
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: bucabay</title>
		<link>http://mjtsai.com/blog/2004/07/15/php-5-object-references/comment-page-1/#comment-515541</link>
		<dc:creator>bucabay</dc:creator>
		<pubDate>Sun, 16 Aug 2009 14:58:59 +0000</pubDate>
		<guid isPermaLink="false">/?p=862#comment-515541</guid>
		<description>In PHP5, assigning objects to variables creates a copy-on-write reference to the object. However, the properties of that object are references.

$obj = new StdClass; // this is a copy-on-write
$obj-&gt;property = &#039;hi&#039;; // the property is a reference

$obj2 = $obj; // copy on write

$obj2-&gt;property = &#039;bye&#039;; // $obj-&gt;property also changed since it is a reference

$obj2 = null; // $obj2 changed, thus it has to be copied, $obj stays the same

$property = $obj-&gt;property; // copy-on-write reference

$property = null; // $property changed, thus copied, does not affect $obj-&gt;property; 

In fact as far as I can see, all assignment is copy-on-write, even __clone(), unless specifically referenced with &amp; or an object property (that has more then one reference to it). 
The notion that PHP5 objects are assigned by reference, or passed by reference is not true, only the properties of that object are. All variables seem to be passed or assigned as copy-on-write unless using &amp;.</description>
		<content:encoded><![CDATA[<p>In PHP5, assigning objects to variables creates a copy-on-write reference to the object. However, the properties of that object are references.</p>
<p>$obj = new StdClass; // this is a copy-on-write<br />
$obj-&gt;property = 'hi'; // the property is a reference</p>
<p>$obj2 = $obj; // copy on write</p>
<p>$obj2-&gt;property = 'bye'; // $obj-&gt;property also changed since it is a reference</p>
<p>$obj2 = null; // $obj2 changed, thus it has to be copied, $obj stays the same</p>
<p>$property = $obj-&gt;property; // copy-on-write reference</p>
<p>$property = null; // $property changed, thus copied, does not affect $obj-&gt;property; </p>
<p>In fact as far as I can see, all assignment is copy-on-write, even __clone(), unless specifically referenced with &amp; or an object property (that has more then one reference to it).<br />
The notion that PHP5 objects are assigned by reference, or passed by reference is not true, only the properties of that object are. All variables seem to be passed or assigned as copy-on-write unless using &amp;.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: destroy referenced objects &#124; keyongtech</title>
		<link>http://mjtsai.com/blog/2004/07/15/php-5-object-references/comment-page-1/#comment-468349</link>
		<dc:creator>destroy referenced objects &#124; keyongtech</dc:creator>
		<pubDate>Sun, 18 Jan 2009 17:14:18 +0000</pubDate>
		<guid isPermaLink="false">/?p=862#comment-468349</guid>
		<description>[...] I thought, with php5, &#039;&amp;&#039; was no longer needed...  The solution is explained in this article:  http://mjtsai.com/blog/2004/07/15/ph...ct-references/  It appears that my problem boils down to a misapprehension of what &quot;passing an object by [...]</description>
		<content:encoded><![CDATA[<p>[...] I thought, with php5, '&amp;' was no longer needed...  The solution is explained in this article:  <a href="http://mjtsai.com/blog/2004/07/15/ph...ct-references/" rel="nofollow">http://mjtsai.com/blog/2004/07/15/ph...ct-references/</a>  It appears that my problem boils down to a misapprehension of what &quot;passing an object by [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Adrian</title>
		<link>http://mjtsai.com/blog/2004/07/15/php-5-object-references/comment-page-1/#comment-271485</link>
		<dc:creator>Adrian</dc:creator>
		<pubDate>Thu, 03 Apr 2008 05:44:46 +0000</pubDate>
		<guid isPermaLink="false">/?p=862#comment-271485</guid>
		<description>I think there is a difference between 
$a =&amp; $b;
and
$a = &amp;$b;</description>
		<content:encoded><![CDATA[<p>I think there is a difference between<br />
$a =&amp; $b;<br />
and<br />
$a = &amp;$b;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Roy</title>
		<link>http://mjtsai.com/blog/2004/07/15/php-5-object-references/comment-page-1/#comment-265138</link>
		<dc:creator>Roy</dc:creator>
		<pubDate>Sun, 23 Mar 2008 20:47:26 +0000</pubDate>
		<guid isPermaLink="false">/?p=862#comment-265138</guid>
		<description>This might be a late comment, but I believe you&#039;re confused between passing and assigning.
example:
$a = &#039;frop&#039;;
$a = $b;
Will put the value $a has into $b.
$a =&amp; $b;
Will make a reference, so that $a and $b both reference to the same value.

Passing is a whole other thing.
function foo( $text ) {
  //bla
}
Will copy a string/int/double (scalars).
This is passing something to a function.
in PHP5 the default action is pass by reference, FOR OBJECTS.
So, when you pass an object (to an function or something), then it will make a reference by default.
So just keep in mind that there is a difference between assigning and passing.</description>
		<content:encoded><![CDATA[<p>This might be a late comment, but I believe you're confused between passing and assigning.<br />
example:<br />
$a = 'frop';<br />
$a = $b;<br />
Will put the value $a has into $b.<br />
$a =&amp; $b;<br />
Will make a reference, so that $a and $b both reference to the same value.</p>
<p>Passing is a whole other thing.<br />
function foo( $text ) {<br />
  //bla<br />
}<br />
Will copy a string/int/double (scalars).<br />
This is passing something to a function.<br />
in PHP5 the default action is pass by reference, FOR OBJECTS.<br />
So, when you pass an object (to an function or something), then it will make a reference by default.<br />
So just keep in mind that there is a difference between assigning and passing.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Freebee</title>
		<link>http://mjtsai.com/blog/2004/07/15/php-5-object-references/comment-page-1/#comment-259714</link>
		<dc:creator>Freebee</dc:creator>
		<pubDate>Sat, 15 Mar 2008 12:27:33 +0000</pubDate>
		<guid isPermaLink="false">/?p=862#comment-259714</guid>
		<description>When assigning an already created instance of an object to a new variable, the new variable will access the same instance as the object that was assigned. This behaviour is the same when passing instances to a function. A new instance of an already created object can be made by cloning it</description>
		<content:encoded><![CDATA[<p>When assigning an already created instance of an object to a new variable, the new variable will access the same instance as the object that was assigned. This behaviour is the same when passing instances to a function. A new instance of an already created object can be made by cloning it</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: winterheat</title>
		<link>http://mjtsai.com/blog/2004/07/15/php-5-object-references/comment-page-1/#comment-147225</link>
		<dc:creator>winterheat</dc:creator>
		<pubDate>Thu, 27 Sep 2007 13:18:38 +0000</pubDate>
		<guid isPermaLink="false">/?p=862#comment-147225</guid>
		<description>and that $obj =&amp; $obj2
is just like, treat us as synonyms...</description>
		<content:encoded><![CDATA[<p>and that $obj =&amp; $obj2<br />
is just like, treat us as synonyms...</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: winterheat</title>
		<link>http://mjtsai.com/blog/2004/07/15/php-5-object-references/comment-page-1/#comment-147224</link>
		<dc:creator>winterheat</dc:creator>
		<pubDate>Thu, 27 Sep 2007 13:17:35 +0000</pubDate>
		<guid isPermaLink="false">/?p=862#comment-147224</guid>
		<description>holy cow, so in PHP5, $obj = $obj2
and $obj =&amp; $obj2 are different things...

so my understanding is that $obj = $obj2 in PHP 5 is the same as in Ruby or Java.</description>
		<content:encoded><![CDATA[<p>holy cow, so in PHP5, $obj = $obj2<br />
and $obj =&amp; $obj2 are different things...</p>
<p>so my understanding is that $obj = $obj2 in PHP 5 is the same as in Ruby or Java.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jumble</title>
		<link>http://mjtsai.com/blog/2004/07/15/php-5-object-references/comment-page-1/#comment-130451</link>
		<dc:creator>Jumble</dc:creator>
		<pubDate>Tue, 28 Aug 2007 17:39:07 +0000</pubDate>
		<guid isPermaLink="false">/?p=862#comment-130451</guid>
		<description>I found an interesting article which exactly describes what happens internaly on assigning values:
http://derickrethans.nl/files/phparch-php-variables-article.pdf
Objects aren&#039;t mentioned there, but it IMHO uses the same mechanism.</description>
		<content:encoded><![CDATA[<p>I found an interesting article which exactly describes what happens internaly on assigning values:<br />
<a href="http://derickrethans.nl/files/phparch-php-variables-article.pdf" rel="nofollow">http://derickrethans.nl/files/phparch-php-variables-article.pdf</a><br />
Objects aren't mentioned there, but it IMHO uses the same mechanism.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mansi</title>
		<link>http://mjtsai.com/blog/2004/07/15/php-5-object-references/comment-page-1/#comment-124630</link>
		<dc:creator>Mansi</dc:creator>
		<pubDate>Thu, 16 Aug 2007 10:45:51 +0000</pubDate>
		<guid isPermaLink="false">/?p=862#comment-124630</guid>
		<description>i does not agree with kyle.I think they act as the hard links</description>
		<content:encoded><![CDATA[<p>i does not agree with kyle.I think they act as the hard links</p>
]]></content:encoded>
	</item>
</channel>
</rss>
