<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Cloning &#8211; stoimen&#039;s web log</title>
	<atom:link href="/tag/cloning/feed/" rel="self" type="application/rss+xml" />
	<link></link>
	<description>on web development</description>
	<lastBuildDate>Tue, 13 Feb 2018 08:18:15 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=5.0.3</generator>
	<item>
		<title>Object Cloning and Passing by Reference in PHP</title>
		<link>/2011/10/27/object-cloning-and-passing-by-reference-in-php/</link>
		<comments>/2011/10/27/object-cloning-and-passing-by-reference-in-php/#comments</comments>
		<pubDate>Thu, 27 Oct 2011 14:25:17 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[micro tutorial]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[Clone]]></category>
		<category><![CDATA[Cloning]]></category>
		<category><![CDATA[Comparison of programming languages]]></category>
		<category><![CDATA[Computer programming]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[Curly bracket programming languages]]></category>
		<category><![CDATA[Java programming language]]></category>
		<category><![CDATA[PHP programming language]]></category>
		<category><![CDATA[Procedural programming languages]]></category>
		<category><![CDATA[Scripting languages]]></category>
		<category><![CDATA[Software engineering]]></category>

		<guid isPermaLink="false">/?p=2408</guid>
		<description><![CDATA[In PHP everything&#8217;s a reference! I&#8217;ve heard it so many times in my practice. No, these words are too strong! Let&#8217;s see some examples. Passing Parameters by Reference Clearly when we pass parameters to a function it&#8217;s not by reference. How to check this? Well, like this. function f($param) { $param++; } $a = 5; &#8230; <a href="/2011/10/27/object-cloning-and-passing-by-reference-in-php/" class="more-link">Continue reading <span class="screen-reader-text">Object Cloning and Passing by Reference in PHP</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2012/08/17/its-not-true-that-php-arrays-are-copied-by-value/" rel="bookmark" title="It&#8217;s Not True that PHP Arrays are Copied by Value">It&#8217;s Not True that PHP Arrays are Copied by Value </a></li>
<li><a href="/2012/04/26/php-strings-dont-need-quotes/" rel="bookmark" title="PHP Strings Don&#8217;t Need Quotes">PHP Strings Don&#8217;t Need Quotes </a></li>
<li><a href="/2011/10/20/some-notes-on-the-object-oriented-model-of-php/" rel="bookmark" title="Some Notes on the Object-oriented Model of PHP">Some Notes on the Object-oriented Model of PHP </a></li>
<li><a href="/2010/06/11/friday-algorithms-quicksort-difference-between-php-and-javascript/" rel="bookmark" title="Friday Algorithms: Quicksort &#8211; Difference Between PHP and JavaScript">Friday Algorithms: Quicksort &#8211; Difference Between PHP and JavaScript </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<p>In <a href="/category/php/" title="PHP at stoimen.com">PHP </a>everything&#8217;s a reference! I&#8217;ve heard it so many times in my practice. No, these words are too strong! Let&#8217;s see some examples.<br />
<figure id="attachment_2432" style="width: 480px" class="wp-caption alignnone"><a href="/wp-content/uploads/2011/10/ampersand.jpg"><img src="/wp-content/uploads/2011/10/ampersand.jpg" alt="Passing by reference in PHP can be tricky!" title="ampersand" width="480" height="480" class="size-full wp-image-2432" srcset="/wp-content/uploads/2011/10/ampersand.jpg 480w, /wp-content/uploads/2011/10/ampersand-150x150.jpg 150w, /wp-content/uploads/2011/10/ampersand-300x300.jpg 300w" sizes="(max-width: 480px) 100vw, 480px" /></a><figcaption class="wp-caption-text">Some developers think that everything&#039;s passed by reference in PHP.</figcaption></figure></p>
<h2>Passing Parameters by Reference</h2>
<p>Clearly when we pass parameters to a function it&#8217;s not by reference. How to check this? Well, like this.</p>
<pre lang="PHP">
function f($param)
{
	$param++;
}

$a = 5;
f($a);

echo $a;
</pre>
<p>Now the value of $a equals 5. If it were passed by reference, it would be 6. With a little change of the code we can get it.</p>
<pre lang="PHP">
function f(&$param)
{
	$param++;
}

$a = 5;
f($a);

echo $a;
</pre>
<p>Now the variable&#8217;s value is 6. </p>
<p>So far, so good. Now what about copying objects?<br />
<span id="more-2408"></span></p>
<h2>Objects: A Copy or a Cloning?</h2>
<p>We can check whether by assigning an object to a variable a reference or a copy of the object is passed.</p>
<pre lang="PHP">
class C
{
	public $myvar = 10;
}

$a = new C();
$b = $a;

$b->myvar = 20;

// 20, not 10
echo $a->myvar;
</pre>
<p>The last line outputs 20! This makes it clear. By assigning an object to a variable PHP pass its reference. To make a copy there&#8217;s another approach. We need to change $b = $a, to $b = clone $a;</p>
<pre lang="PHP" escaped="true">
class C
{
	public $myvar = 10;
}

$a = new C();
$b = clone $a;

$b->myvar = 20;

// 10
echo $a->myvar;
</pre>
<h2>Arrays by Reference</h2>
<p>What about arrays? What if I assign an array to a variable?</p>
<pre lang="PHP">
$a = array(20);

$b = $a;
$b[0] = 30;

var_dump($a);
</pre>
<p>What do you think is the value of $a[0]? Well, the answer is: 20! So $b is a copy of the array &#8220;a&#8221;. Instead you should assign explicitly its reference to make &#8220;b&#8221; point to &#8220;a&#8221;.</p>
<pre lang="PHP">
$a = array(20);

$b = &$a;
$b[0] = 30;

var_dump($a);
</pre>
<p>Now $a[0] equals 30!</p>
<p>I think this could be useful!</p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2012/08/17/its-not-true-that-php-arrays-are-copied-by-value/" rel="bookmark" title="It&#8217;s Not True that PHP Arrays are Copied by Value">It&#8217;s Not True that PHP Arrays are Copied by Value </a></li>
<li><a href="/2012/04/26/php-strings-dont-need-quotes/" rel="bookmark" title="PHP Strings Don&#8217;t Need Quotes">PHP Strings Don&#8217;t Need Quotes </a></li>
<li><a href="/2011/10/20/some-notes-on-the-object-oriented-model-of-php/" rel="bookmark" title="Some Notes on the Object-oriented Model of PHP">Some Notes on the Object-oriented Model of PHP </a></li>
<li><a href="/2010/06/11/friday-algorithms-quicksort-difference-between-php-and-javascript/" rel="bookmark" title="Friday Algorithms: Quicksort &#8211; Difference Between PHP and JavaScript">Friday Algorithms: Quicksort &#8211; Difference Between PHP and JavaScript </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2011/10/27/object-cloning-and-passing-by-reference-in-php/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
