<?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>Geek Files &#187; PHP5 Magic Methods</title>
	<atom:link href="http://www.sunilb.com/category/php/php5-magic-methods/feed" rel="self" type="application/rss+xml" />
	<link>http://www.sunilb.com</link>
	<description>Question Everything - that&#039;s the only way to learn</description>
	<lastBuildDate>Mon, 30 Jan 2012 04:59:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.4</generator>
		<item>
		<title>PHP5 Tutorial &#8211; Magic Methods &#8211; __clone() method</title>
		<link>http://www.sunilb.com/php/php5-oops-tutorial-magic-methods-__clone-method</link>
		<comments>http://www.sunilb.com/php/php5-oops-tutorial-magic-methods-__clone-method#comments</comments>
		<pubDate>Mon, 05 Nov 2007 16:32:50 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[PHP Tutorials]]></category>
		<category><![CDATA[PHP5 Magic Methods]]></category>
		<category><![CDATA[PHP5 OOPS Tutorials]]></category>

		<guid isPermaLink="false">http://www.sunilb.com/php/php-tutorials/php5-oops-tutorial-magic-methods-__clone-method</guid>
		<description><![CDATA[Before I begin to explain the use of a __clone() method, lets try and understand what does object cloning mean. To clone an object means to create a duplicate of an object. With regular variables $a = $b means that a new variable $a gets created that contains the value of $b. This means that [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.sunilb.com%2Fphp%2Fphp5-oops-tutorial-magic-methods-__clone-method"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.sunilb.com%2Fphp%2Fphp5-oops-tutorial-magic-methods-__clone-method&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Before I begin to explain the use of a __clone() method, lets try and understand what does<strong> object cloning</strong> mean.</p>
<p>To <strong>clone an object</strong> means to create a duplicate of an object. With regular variables $a = $b means that a new variable $a gets created that contains the value of $b. This means that 2 variables get created.</p>
<p>With objects $obj2 = $obj1 does not mean that a new object i.e. $obj2 gets created. When we execute $obj2 = $obj1, the reference of $obj1 is assigned to $obj2. This means that $obj1 and $obj2 point to the same memory space. Look at the diagram below.</p>
<p><span id="more-52"></span></p>
<p align="center"><img src="http://www.sunilb.com/wp-content/uploads/2007/11/clone1.gif" alt="PHP5 Tutorial - Magic Method - __clone() method" /></p>
<p>Lets look at an example where only references are assigned to another object:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Customer <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$name</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setName<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">name</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$name</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">name</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$c1</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Customer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$c1</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Sunil&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$c2</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$c1</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//only reference or memory assigned to $c2</span>
&nbsp;
<span style="color: #000088;">$c2</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Vishal&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$c1</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$c2</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<p><strong>Output:</strong><br />
Vishal<br />
Vishal</p>
<blockquote><p>In the above example, $c2 has the reference of $c1; therefore, when you set a new name in the $c2 object &#8211; $c1 object changes as well. Therefore, when an object is assigned as a reference; changes made to one object are also reflected in the other.</p></blockquote>
<p>Therefore, to create a new $obj2 object we must clone an object to create a new one. To clone an <strong><a href="http://www.sunilb.com/php/php-tutorials/php5-oops-tutorial-learn-to-creating-a-php5-class-object" title="PHP5 Tutorial - Learn to Create a PHP5 Class Object" target="_blank">PHP5 Object</a></strong> a special keyword i.e. <strong>clone </strong>is used. Example below:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">        <span style="color: #000088;">$obj2</span> <span style="color: #339933;">=</span> clone <span style="color: #000088;">$obj1</span><span style="color: #339933;">;</span></pre></div></div>

<p>After the above line is executed $obj2 with a new memory space is created with the data members having the same value as that of $obj1. This is also referred to as <strong>shallow copy</strong>.</p>
<p><code><br />
<script type="text/javascript"><!--
google_ad_client = "pub-9205249129147978";
google_ad_width = 468;
google_ad_height = 60;
google_ad_format = "468x60_as";
google_ad_type = "text_image";
//2007-10-26: Sunilb.com 468x60
google_ad_channel = "7135663694";
google_color_border = "FFFFFF";
google_color_bg = "FFFFFF";
google_color_link = "0000FF";
google_color_text = "0F0F0F";
google_color_url = "CCCCCC";
//-->
</script><br />
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script><br />
</code></p>
<p align="center"><img src="http://www.sunilb.com/wp-content/uploads/2007/11/clone2.gif" alt="PHP5 Tutorial - Magic Method - __clone() method" /></p>
<blockquote><p>The above technique works with a class having data members that are of intrinsic type i.e. int, boolean, string, float, etc.. However, this technique will not work with a class that has a data member which is an object of another class. In such a scenario, the cloned object continues to share the reference of the data member object of the class that was cloned.</p></blockquote>
<p>So, how do we resolve this issue? Doing a regular <strong>shallow copy</strong> won&#8217;t help us. To allow aggregated objects (i.e. data members that are objects of another class) to also get cloned properly we need to use the concept of &#8216;<strong>deep copy</strong>&#8216; as opposed to &#8216;<strong>shallow copy</strong>&#8216;. To implement a &#8216;<strong>deep copy</strong>&#8216; you should implement the <strong>magic method</strong> __clone().</p>
<p>You could also provide the implementation of __clone() <strong>magic method</strong> even when you don&#8217;t have an aggregated object. You would want to do this for providing necessary clean up operations, conversions or validations.</p>
<p>Lets explore a very simple example of cloning intrinsic data types:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Customer <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$name</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setName<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">name</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$name</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">name</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __clone<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$c</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Customer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$c</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setName</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$c</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$c1</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Customer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$c1</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Sunil&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$c2</span> <span style="color: #339933;">=</span> clone <span style="color: #000088;">$c1</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//new object $c2 created</span>
&nbsp;
<span style="color: #000088;">$c2</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Vishal&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$c1</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$c2</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Output:<br />
Sunil<br />
Vishal</p>
<blockquote><p>In the above example, observe the line where the statement $c2 = clone $c1 is executed. This is internally represented as $c2 = $c1.__clone(). However, you cannot explicitly call the __clone() method on an object as the __clone() is automatically called. Now that $c1 and $c2 are two individual objects, changes made to one object is not reflected in the other.</p></blockquote>
<p><code><br />
<script type="text/javascript"><!--
google_ad_client = "pub-9205249129147978";
google_ad_width = 468;
google_ad_height = 60;
google_ad_format = "468x60_as";
google_ad_type = "text_image";
//2007-10-26: Sunilb.com 468x60
google_ad_channel = "7135663694";
google_color_border = "FFFFFF";
google_color_bg = "FFFFFF";
google_color_link = "0000FF";
google_color_text = "0F0F0F";
google_color_url = "CCCCCC";
//-->
</script><br />
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script><br />
</code></p>
<p><strong>Cloning aggregate objects (i.e. data members that are objects of another class)</strong></p>
<p>To clone a class having aggregated objects, you should perform &#8216;<strong>deep copy</strong>&#8216;. Please refer to the example below:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Order <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$order_id</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$customer</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setOrderId<span style="color: #009900;">&#40;</span><span style="color: #000088;">$order_id</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">order_id</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$order_id</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getOrderId<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">order_id</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setCustomer<span style="color: #009900;">&#40;</span>Customer <span style="color: #000088;">$customer</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">customer</span> <span style="color: #339933;">=</span> clone <span style="color: #000088;">$customer</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getCustomer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">customer</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __clone<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #000088;">$o</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Order<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$o</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setOrderId</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">order_id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
		<span style="color: #666666; font-style: italic;">//force a copy of the same object to itself, otherwise</span>
		<span style="color: #666666; font-style: italic;">//it takes the same instance. Seems like a bug to me</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">customer</span> <span style="color: #339933;">=</span> clone <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">customer</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$o</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setCustomer</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">customer</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$o</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Customer <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$name</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setName<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">name</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$name</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">name</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __clone<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$c</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Customer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$c</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setName</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$c</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$c</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Customer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$c</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Sunil&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$o1</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Order<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$o1</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setOrderId</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;OD0001&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$o1</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setCustomer</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$c</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$o2</span> <span style="color: #339933;">=</span> clone <span style="color: #000088;">$o1</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$o2</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getCustomer</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Vishal&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">var_dump</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$c</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">var_dump</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$o1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">var_dump</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$o2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><strong>Output:</strong><br />
object(Customer)#1 (1) {<br />
[â€name:privateâ€]=><br />
string(5) â€œSunilâ€<br />
}<br />
object(Order)#2 (2) {<br />
[â€order_id:privateâ€]=><br />
string(6) â€œOD0001â€³<br />
[â€customer:privateâ€]=><br />
object(Customer)#3 (1) {<br />
[â€name:privateâ€]=><br />
string(5) â€œSunilâ€<br />
}<br />
}<br />
object(Order)#4 (2) {<br />
[â€order_id:privateâ€]=><br />
string(6) â€œOD0001â€³<br />
[â€customer:privateâ€]=><br />
object(Customer)#6 (1) {<br />
[â€name:privateâ€]=><br />
string(6) â€œVishalâ€<br />
}<br />
}</p>
<blockquote><p>In the above example both $o1 and $o2 have their own set of customer objects, therefore changes made to one object is not reflected in another. This example implements the concepts of &#8216;<strong>deep copy</strong>&#8216;.</p></blockquote>
<blockquote><p>A special note on $this->customer = clone $this->customer; For some reason it is necessary to do this for proper working of aggregated cloning.</p></blockquote>
<p>I hope this tutorial was helpful. Please feel free to leave behind any comments or questions that you might have.</p>
<p><strong>Related Posts on PHP5 Tutorial &#8211; Object Oriented Programming (OOPS)</strong></p>
<ol>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-oops-tutorial-learn-to-create-a-php5-class" title="PHP5 Tutorial - Learn to create a PHP5 Class" target="_blank">PHP5 Tutorial &#8211; Learn to create a PHP5 Class</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-oops-tutorial-learn-to-creating-a-php5-class-object" title="PHP5 Tutorial - Learn to Create a PHP5 Class Object" target="_blank">PHP5 Tutorial &#8211; Learn to Create a PHP5 Class Object</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-oops-tutorial-defining-attributes-of-a-php5-class" title="PHP5 Tutorial - Defining Attributes of a PHP5 Class" target="_blank">PHP5 Tutorial &#8211; Defining Attributes of a PHP5 Class</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-oops-tutorial-defining-methods-of-a-php5-class" title="PHP5 Tutorial - Defining Methods of a PHP5 Class" target="_blank">PHP5 Tutorial &#8211; Defining Methods of a PHP5 Class</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-tutorial-creating-a-php5-constructor-__construct" title="PHP5 Tutorial - Creating a PHP5 Constructor __construct()" target="_blank">PHP5 Tutorial &#8211; Creating a PHP5 Constructor __construct()</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-tutorial-oops-creating-a-php5-destructor-__destruct" title="PHP5 Tutorial OOPS - Creating a PHP5 Destructor __destruct()" target="_blank">PHP5 Tutorial OOPS &#8211; Creating a PHP5 Destructor __destruct()</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-tutorial-oops-php5-class-access-specifiers-public-private-and-protected" title="PHP5 Tutorial OOPS - PHP5 Class Access Specifiers - public, private and protected" target="_blank">PHP5 Tutorial OOPS &#8211; PHP5 Class Access Specifiers &#8211; public, private and protected</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-tutorial-magic-methods-__tostring-method" title="PHP5 Tutorial - Magic Methods - __toString() method" target="_blank">PHP5 Tutorial &#8211; Magic Methods &#8211; __toString() method</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-oops-tutorial-__get-and-__set" title="PHP5 Tutorial __get() and __set()" target="_blank">PHP5 Tutorial &#8211; Magic Methods &#8211; __get() and __set()</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-oops-tutorial-magic-methods-__isset-and-__unset" title="PHP5 Tutorial - Magic Methods - __isset() and __unset()" target="_blank">PHP5 Tutorial &#8211; Magic Methods &#8211; __isset() and __unset()</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-oops-tutorial-magic-methods-__call-method" title="PHP5 Tutorial - Magic Methods - __call() method" target="_blank">PHP5 Tutorial &#8211; Magic Methods &#8211; __call() method</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-oops-tutorial-magic-methods-__autoload-method" title="PHP5 Tutorial - Magic Methods - __autoload() method" target="_blank">PHP5 Tutorial &#8211; Magic Methods &#8211; __autoload() method</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-oops-tutorial-magic-methods-__sleep-and-__wakeup" title="PHP5 Tutorial - Magic Methods - __sleep() and __wakeup()" target="_blank">PHP5 Tutorial &#8211; Magic Methods &#8211; __sleep() and __wakeup()</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-oops-tutorial-magic-methods-__clone-method" title="PHP5 Tutorial - Magic Methods - __clone() method" target="_blank">PHP5 Tutorial &#8211; Magic Methods &#8211; __clone() method</a></li>
</ol>

]]></content:encoded>
			<wfw:commentRss>http://www.sunilb.com/php/php5-oops-tutorial-magic-methods-__clone-method/feed</wfw:commentRss>
		<slash:comments>26</slash:comments>
		</item>
		<item>
		<title>PHP5 Tutorial &#8211; Magic Methods &#8211; __sleep() and __wakeup()</title>
		<link>http://www.sunilb.com/php/php5-oops-tutorial-magic-methods-__sleep-and-__wakeup</link>
		<comments>http://www.sunilb.com/php/php5-oops-tutorial-magic-methods-__sleep-and-__wakeup#comments</comments>
		<pubDate>Sat, 03 Nov 2007 11:14:19 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[PHP Tutorials]]></category>
		<category><![CDATA[PHP5 Magic Methods]]></category>
		<category><![CDATA[PHP5 OOPS Tutorials]]></category>

		<guid isPermaLink="false">http://www.sunilb.com/php/php-tutorials/php5-oops-tutorial-magic-methods-__sleep-and-__wakeup</guid>
		<description><![CDATA[The magic method __sleep() and __wakeup() is called when an object is serialized. The magic method __sleep() and __wakeup() provides a method to clean up and restore objects before being serialized. Working with the magic method __sleep() __sleep() magic method is called when the object of a class is about to be serialized. This magic [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.sunilb.com%2Fphp%2Fphp5-oops-tutorial-magic-methods-__sleep-and-__wakeup"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.sunilb.com%2Fphp%2Fphp5-oops-tutorial-magic-methods-__sleep-and-__wakeup&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>The <strong>magic method</strong> __sleep() and __wakeup() is called when an object is serialized. The <strong>magic method</strong> __sleep() and __wakeup() provides a method to clean up and restore objects before being serialized.</p>
<p><strong>Working with the magic method __sleep()</strong></p>
<p>__sleep() <strong>magic method</strong> is called when the object of a class is about to be serialized. This magic method __sleep() does not accept any parameter and returns an array. The array should contain a list of class members that should be serialized. This means that if you don&#8217;t wish to serialize a particular class member, you should not include it in the array. Look at the example below:</p>
<p><span id="more-50"></span></p>
<p><code><br />
<script type="text/javascript"><!--
google_ad_client = "pub-9205249129147978";
google_ad_width = 468;
google_ad_height = 60;
google_ad_format = "468x60_as";
google_ad_type = "text_image";
//2007-10-26: Sunilb.com 468x60
google_ad_channel = "7135663694";
google_color_border = "FFFFFF";
google_color_bg = "FFFFFF";
google_color_link = "0000FF";
google_color_text = "0F0F0F";
google_color_url = "CCCCCC";
//-->
</script><br />
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script><br />
</code></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Customer <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$name</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$credit_card_number</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setName<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">name</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$name</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">name</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setCC<span style="color: #009900;">&#40;</span><span style="color: #000088;">$cc</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">credit_card_number</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$cc</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getCC<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">credit_card_number</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __sleep<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;name&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//because of this, only name is serialized</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$c</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Customer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$c</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Sunil&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$c</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setCC</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;1234567890123456&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">serialize</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$c</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$data</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<p><strong>Output:</strong><br />
O:8:&#8221;Customer&#8221;:1:{s:14:&#8221; Customer name&#8221;;s:5:&#8221;Sunil&#8221;;}</p>
<blockquote><p>In the above example, you can see that the serialized string data only contains the name of the Customer Object. This is because the __sleep() <strong>magic method</strong> returned an array containing only the &#8216;name&#8217; data member.</p></blockquote>
<p><code><br />
<script type="text/javascript"><!--
google_ad_client = "pub-9205249129147978";
google_ad_width = 468;
google_ad_height = 60;
google_ad_format = "468x60_as";
google_ad_type = "text_image";
//2007-10-26: Sunilb.com 468x60
google_ad_channel = "7135663694";
google_color_border = "FFFFFF";
google_color_bg = "FFFFFF";
google_color_link = "0000FF";
google_color_text = "0F0F0F";
google_color_url = "CCCCCC";
//-->
</script><br />
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script><br />
</code></p>
<p><strong>Working with the magic method __wakeup()</strong></p>
<p>__wakeup() <strong>magic method</strong> is the opposite of the __sleep() method. It is called when the object of a class is about to be unserialized. This magic method __wakeup() does not accept any parameter nor returns anything. The __wakeup() method is responsible for setup operations after an object has been unserialized. Look at the example below:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Customer <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$name</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$credit_card_number</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setName<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">name</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$name</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">name</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setCC<span style="color: #009900;">&#40;</span><span style="color: #000088;">$cc</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">credit_card_number</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$cc</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getCC<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">credit_card_number</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __sleep<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;name&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __wakeup<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">name</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">&quot;Sunil&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;">//you would ideally fetch CC data from Database</span>
			<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">credit_card_number</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;1234567890123456&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$c</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Customer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$c</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Sunil&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$c</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setCC</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;1234567890123456&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">serialize</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$c</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">var_dump</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">unserialize</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><strong>Output:</strong><br />
object(Customer)#2 (2) {<br />
[â€name:privateâ€]=><br />
string(5) â€œSunilâ€<br />
[â€credit_card_number:privateâ€]=><br />
string(16) â€œ1234567890123456â€³<br />
}</p>
<blockquote><p>In the above example, you can see that after the $c object has been serialized and the output stored in $data variable, we use the $data variable and pass it to the unserialize(). Before the object is unserizlied and object created, the __wakeup() method is called. In the __wakeup() method you should ideally make a database call to fetch data of the missing member variable.</p></blockquote>
<p>Please feel free to leave behind any comments or questions that you might have.</p>
<p><strong>Related Posts on PHP5 Tutorial &#8211; Object Oriented Programming (OOPS)</strong></p>
<ol>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-oops-tutorial-learn-to-create-a-php5-class" title="PHP5 Tutorial - Learn to create a PHP5 Class" target="_blank">PHP5 Tutorial &#8211; Learn to create a PHP5 Class</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-oops-tutorial-learn-to-creating-a-php5-class-object" title="PHP5 Tutorial - Learn to Create a PHP5 Class Object" target="_blank">PHP5 Tutorial &#8211; Learn to Create a PHP5 Class Object</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-oops-tutorial-defining-attributes-of-a-php5-class" title="PHP5 Tutorial - Defining Attributes of a PHP5 Class" target="_blank">PHP5 Tutorial &#8211; Defining Attributes of a PHP5 Class</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-oops-tutorial-defining-methods-of-a-php5-class" title="PHP5 Tutorial - Defining Methods of a PHP5 Class" target="_blank">PHP5 Tutorial &#8211; Defining Methods of a PHP5 Class</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-tutorial-creating-a-php5-constructor-__construct" title="PHP5 Tutorial - Creating a PHP5 Constructor __construct()" target="_blank">PHP5 Tutorial &#8211; Creating a PHP5 Constructor __construct()</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-tutorial-oops-creating-a-php5-destructor-__destruct" title="PHP5 Tutorial OOPS - Creating a PHP5 Destructor __destruct()" target="_blank">PHP5 Tutorial OOPS &#8211; Creating a PHP5 Destructor __destruct()</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-tutorial-oops-php5-class-access-specifiers-public-private-and-protected" title="PHP5 Tutorial OOPS - PHP5 Class Access Specifiers - public, private and protected" target="_blank">PHP5 Tutorial OOPS &#8211; PHP5 Class Access Specifiers &#8211; public, private and protected</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-tutorial-magic-methods-__tostring-method" title="PHP5 Tutorial - Magic Methods - __toString() method" target="_blank">PHP5 Tutorial &#8211; Magic Methods &#8211; __toString() method</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-oops-tutorial-__get-and-__set" title="PHP5 Tutorial __get() and __set()" target="_blank">PHP5 Tutorial &#8211; Magic Methods &#8211; __get() and __set()</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-oops-tutorial-magic-methods-__isset-and-__unset" title="PHP5 Tutorial - Magic Methods - __isset() and __unset()" target="_blank">PHP5 Tutorial &#8211; Magic Methods &#8211; __isset() and __unset()</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-oops-tutorial-magic-methods-__call-method" title="PHP5 Tutorial - Magic Methods - __call() method" target="_blank">PHP5 Tutorial &#8211; Magic Methods &#8211; __call() method</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-oops-tutorial-magic-methods-__autoload-method" title="PHP5 Tutorial - Magic Methods - __autoload() method" target="_blank">PHP5 Tutorial &#8211; Magic Methods &#8211; __autoload() method</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-oops-tutorial-magic-methods-__sleep-and-__wakeup" title="PHP5 Tutorial - Magic Methods - __sleep() and __wakeup()" target="_blank">PHP5 Tutorial &#8211; Magic Methods &#8211; __sleep() and __wakeup()</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-oops-tutorial-magic-methods-__clone-method" title="PHP5 Tutorial - Magic Methods - __clone() method" target="_blank">PHP5 Tutorial &#8211; Magic Methods &#8211; __clone() method</a></li>
</ol>

]]></content:encoded>
			<wfw:commentRss>http://www.sunilb.com/php/php5-oops-tutorial-magic-methods-__sleep-and-__wakeup/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>PHP5 Tutorial &#8211; Magic Methods &#8211; __autoload() method</title>
		<link>http://www.sunilb.com/php/php5-oops-tutorial-magic-methods-__autoload-method</link>
		<comments>http://www.sunilb.com/php/php5-oops-tutorial-magic-methods-__autoload-method#comments</comments>
		<pubDate>Sat, 03 Nov 2007 09:34:31 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[PHP Tutorials]]></category>
		<category><![CDATA[PHP5 Magic Methods]]></category>
		<category><![CDATA[PHP5 OOPS Tutorials]]></category>
		<category><![CDATA[Magic Methods]]></category>
		<category><![CDATA[PHP Tutorial]]></category>
		<category><![CDATA[PHP5]]></category>
		<category><![CDATA[PHP5 OOPS]]></category>
		<category><![CDATA[PHP5 Tutorial]]></category>
		<category><![CDATA[PHP5 Tutorials]]></category>

		<guid isPermaLink="false">http://www.sunilb.com/php/php-tutorials/php5-oops-tutorial-magic-methods-__autoload-method</guid>
		<description><![CDATA[This tutorial will teach you how and when to use the magic method __autoload(). The magic method __autoload() function is a convenience that allows you to use classes without having to explicitly write code to include them. The magic method __autoload() is not included in your class definition as this is to be called once [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.sunilb.com%2Fphp%2Fphp5-oops-tutorial-magic-methods-__autoload-method"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.sunilb.com%2Fphp%2Fphp5-oops-tutorial-magic-methods-__autoload-method&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>This tutorial will teach you how and when to use the <strong>magic method</strong> __autoload().</p>
<p>The <strong>magic method</strong> __autoload() function is a convenience that allows you to use classes without having to explicitly write code to include them.</p>
<p>The <strong>magic method</strong> __autoload() is not included in your class definition as this is to be called once in a script. The best place to put the autoload() file is in your configuration file which is loaded in all your other scripts.<br />
<span id="more-49"></span></p>
<p><code><br />
<script type="text/javascript"><!--
google_ad_client = "pub-9205249129147978";
google_ad_width = 468;
google_ad_height = 60;
google_ad_format = "468x60_as";
google_ad_type = "text_image";
//2007-10-26: Sunilb.com 468x60
google_ad_channel = "7135663694";
google_color_border = "FFFFFF";
google_color_bg = "FFFFFF";
google_color_link = "0000FF";
google_color_text = "0F0F0F";
google_color_url = "CCCCCC";
//-->
</script><br />
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script><br />
</code></p>
<p>Many people debate that the <strong>magic method</strong> __autoload() causes a performance overhead. Well, that is not the case. There is no performance penalty to pay. In fact, there may be performance improvements if not all <strong>classes</strong> are used all the time. This is explained below.</p>
<p>Using the <strong>magic method</strong> __autoload has the beneficial side effect of requiring strict naming conventions for files that hold class definitions.</p>
<p><strong>Look at the example below:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">include</span> â€œcustomer<span style="color: #339933;">.</span>phpâ€<span style="color: #339933;">;</span>
<span style="color: #b1b100;">include</span> â€œorders<span style="color: #339933;">.</span>phpâ€<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$c</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Customer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<blockquote><p>In the example displayed above, an instance of class Customer is created. Therefore, we only need the customers.php file. The file orders.php is not needed. This means that we should only have included the customer.php file. But, what if during execution on the basis of a condition, an instance of class Orders would have to be created. Therefore you need to include both the files i.e. customer.php and orders.php</p></blockquote>
<p>But this causes performance issues. Each time the above script is executed, orders.php is included. To avoid this performance hit, we would have to do additional programming to ensure that the file orders.php is loaded only when needed.</p>
<p>This is the reason why <strong>magic method</strong> __autoload() should be used. Look at the example below:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> __autoload<span style="color: #009900;">&#40;</span><span style="color: #000088;">$class</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #b1b100;">require</span> <span style="color: #000088;">$class</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'.php'</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//is substituted as require Customer.php (with capital 'C')</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$c</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Customer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<blockquote><p>In the above program, we don&#8217;t explicitly include customer.php and orders.php file. When an instance of the customer class is to be created, the PHP engine checks to see if the file Customer.php is loaded. It does not raise an warning on finding that Customer.php has not been loaded, it in turn calls the <strong>magic method</strong> __autoload(). The __autoload() magic method accepts a parameter which is the name of the class that needs to be loaded.</p></blockquote>
<p><code><br />
<script type="text/javascript"><!--
google_ad_client = "pub-9205249129147978";
google_ad_width = 468;
google_ad_height = 60;
google_ad_format = "468x60_as";
google_ad_type = "text_image";
//2007-10-26: Sunilb.com 468x60
google_ad_channel = "7135663694";
google_color_border = "FFFFFF";
google_color_bg = "FFFFFF";
google_color_link = "0000FF";
google_color_text = "0F0F0F";
google_color_url = "CCCCCC";
//-->
</script><br />
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script><br />
</code></p>
<p>Therefore, on the line when an instance of the customer class is created i.e. object $c, <strong>magic method</strong> __autoload() is called with the parameter $class containing value &#8216;Customer&#8217;. Within the __autoload() method we call the &#8216;require&#8217; method. The require method tries to load $class.&#8217;php&#8217; file i.e. Customer.php. Therefore, as stated earlier, the __autoload() method has its beneficial side effect of requiring strict file naming convention.</p>
<p>The __autoload() method is called only once for each new class that needs to be loaded. Subsequent instantiation of the Customer class object will not call the __autoload() method again. Therefore, this offers performance improvements in your scripts because, unless the class is needed &#8211; files are not loaded. Therefore, the PHP engine does not have to parse and compile an unnecessary file.</p>
<p>Please feel free to leave behind any comments or questions that you might have.</p>
<p><strong>Related Posts on PHP5 Tutorial &#8211; Object Oriented Programming (OOPS)</strong></p>
<ol>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-oops-tutorial-learn-to-create-a-php5-class" title="PHP5 Tutorial - Learn to create a PHP5 Class" target="_blank">PHP5 Tutorial &#8211; Learn to create a PHP5 Class</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-oops-tutorial-learn-to-creating-a-php5-class-object" title="PHP5 Tutorial - Learn to Create a PHP5 Class Object" target="_blank">PHP5 Tutorial &#8211; Learn to Create a PHP5 Class Object</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-oops-tutorial-defining-attributes-of-a-php5-class" title="PHP5 Tutorial - Defining Attributes of a PHP5 Class" target="_blank">PHP5 Tutorial &#8211; Defining Attributes of a PHP5 Class</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-oops-tutorial-defining-methods-of-a-php5-class" title="PHP5 Tutorial - Defining Methods of a PHP5 Class" target="_blank">PHP5 Tutorial &#8211; Defining Methods of a PHP5 Class</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-tutorial-creating-a-php5-constructor-__construct" title="PHP5 Tutorial - Creating a PHP5 Constructor __construct()" target="_blank">PHP5 Tutorial &#8211; Creating a PHP5 Constructor __construct()</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-tutorial-oops-creating-a-php5-destructor-__destruct" title="PHP5 Tutorial OOPS - Creating a PHP5 Destructor __destruct()" target="_blank">PHP5 Tutorial OOPS &#8211; Creating a PHP5 Destructor __destruct()</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-tutorial-oops-php5-class-access-specifiers-public-private-and-protected" title="PHP5 Tutorial OOPS - PHP5 Class Access Specifiers - public, private and protected" target="_blank">PHP5 Tutorial OOPS &#8211; PHP5 Class Access Specifiers &#8211; public, private and protected</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-tutorial-magic-methods-__tostring-method" title="PHP5 Tutorial - Magic Methods - __toString() method" target="_blank">PHP5 Tutorial &#8211; Magic Methods &#8211; __toString() method</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-oops-tutorial-__get-and-__set" title="PHP5 Tutorial __get() and __set()" target="_blank">PHP5 Tutorial &#8211; Magic Methods &#8211; __get() and __set()</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-oops-tutorial-magic-methods-__isset-and-__unset" title="PHP5 Tutorial - Magic Methods - __isset() and __unset()" target="_blank">PHP5 Tutorial &#8211; Magic Methods &#8211; __isset() and __unset()</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-oops-tutorial-magic-methods-__call-method" title="PHP5 Tutorial - Magic Methods - __call() method" target="_blank">PHP5 Tutorial &#8211; Magic Methods &#8211; __call() method</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-oops-tutorial-magic-methods-__autoload-method" title="PHP5 Tutorial - Magic Methods - __autoload() method" target="_blank">PHP5 Tutorial &#8211; Magic Methods &#8211; __autoload() method</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-oops-tutorial-magic-methods-__sleep-and-__wakeup" title="PHP5 Tutorial - Magic Methods - __sleep() and __wakeup()" target="_blank">PHP5 Tutorial &#8211; Magic Methods &#8211; __sleep() and __wakeup()</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-oops-tutorial-magic-methods-__clone-method" title="PHP5 Tutorial - Magic Methods - __clone() method" target="_blank">PHP5 Tutorial &#8211; Magic Methods &#8211; __clone() method</a></li>
</ol>

]]></content:encoded>
			<wfw:commentRss>http://www.sunilb.com/php/php5-oops-tutorial-magic-methods-__autoload-method/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>PHP5 Tutorial &#8211; Magic Methods &#8211; __call() method</title>
		<link>http://www.sunilb.com/php/php5-oops-tutorial-magic-methods-__call-method</link>
		<comments>http://www.sunilb.com/php/php5-oops-tutorial-magic-methods-__call-method#comments</comments>
		<pubDate>Sat, 03 Nov 2007 08:37:14 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[PHP Tutorials]]></category>
		<category><![CDATA[PHP5 Magic Methods]]></category>
		<category><![CDATA[PHP5 OOPS Tutorials]]></category>

		<guid isPermaLink="false">http://www.sunilb.com/php/php-tutorials/php5-oops-tutorial-magic-methods-__call-method</guid>
		<description><![CDATA[This tutorial will teach you how and when to use the magic method __call(). The magic method __call() is to undeclared methods what __get() and __set() are to undeclared data member. These methods are automatically called internally when the program tires to execute a method that has not been defined within the class at the [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.sunilb.com%2Fphp%2Fphp5-oops-tutorial-magic-methods-__call-method"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.sunilb.com%2Fphp%2Fphp5-oops-tutorial-magic-methods-__call-method&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>This tutorial will teach you how and when to use the <strong>magic method __call()</strong>.</p>
<p>The <strong>magic method __call()</strong> is to undeclared methods what <a href="http://www.sunilb.com/php/php-tutorials/php5-oops-tutorial-__get-and-__set" title="PHP5 Tutorial __get() and __set()" target="_blank">__get() and __set()</a> are to undeclared data member.</p>
<p>These methods are automatically called internally when the program tires to execute a method that has not been defined within the class at the time of development.</p>
<p><span id="more-48"></span></p>
<p>The <strong>magic method __call()</strong> takes two arguments. The first argument is the name of the undeclared method invoked by the program and the second is an array that contains a list of parameters passed to the undeclared array.</p>
<p><code><br />
<script type="text/javascript"><!--
google_ad_client = "pub-9205249129147978";
google_ad_width = 468;
google_ad_height = 60;
google_ad_format = "468x60_as";
google_ad_type = "text_image";
//2007-10-26: Sunilb.com 468x60
google_ad_channel = "7135663694";
google_color_border = "FFFFFF";
google_color_bg = "FFFFFF";
google_color_link = "0000FF";
google_color_text = "0F0F0F";
google_color_url = "CCCCCC";
//-->
</script><br />
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script><br />
</code></p>
<p><strong>Look at the example below:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Customer <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __call<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$args</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #990000;">var_dump</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #990000;">var_dump</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$args</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$c</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Customer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$c</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Sunil&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;Bhatia&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Output:<br />
string(7) â€œsetNameâ€</p>
<p>array(2) {<br />
[0]=><br />
string(5) â€œSunilâ€<br />
[1]=><br />
string(6) â€œBhatiaâ€<br />
}</p>
<p><code><br />
<script type="text/javascript"><!--
google_ad_client = "pub-9205249129147978";
google_ad_width = 468;
google_ad_height = 60;
google_ad_format = "468x60_as";
google_ad_type = "text_image";
//2007-10-26: Sunilb.com 468x60
google_ad_channel = "7135663694";
google_color_border = "FFFFFF";
google_color_bg = "FFFFFF";
google_color_link = "0000FF";
google_color_text = "0F0F0F";
google_color_url = "CCCCCC";
//-->
</script><br />
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script><br />
</code></p>
<blockquote><p>In the example above, an object of the Customer class is created and an undeclared method viz. $c->setName is called. The magic method __call() is internally executed which accepts two parameters. The first parameter â€˜$nameâ€™ contains the name of the method i.e. â€™setNameâ€™ and the second parameter â€˜$argsâ€™ contains the arguments passed to the â€™setNameâ€™ method i.e â€˜Sunilâ€™ &#038; â€˜Bhatiaâ€™.</p></blockquote>
<p>Using this method, you can provide code to handle calls to undeclared method. To disallow programs to call an undeclared method, you should raise an exception from within __call() <strong>magic method</strong>.</p>
<p><strong>Look at the example below:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Customer <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __call<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$args</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		throw <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Undeclared method execution not allowed&quot;</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$c</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Customer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$c</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Sunil&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;Bhatia&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><strong>Output:</strong><br />
Fatal error:  Uncaught exception â€˜Exceptionâ€™ with message â€˜Undeclared method execution not allowedâ€™ in D:sunilbwebsiteprogsmagic_call.php:6<br />
Stack trace:<br />
#0 [internal function]: Customer->__call(â€™setNameâ€™, Array)<br />
#1 D:sunilbwebsiteprogsmagic_call.php(11): Customer->setName(â€™Sunilâ€™, â€˜Bhatiaâ€™)<br />
#2 {main}<br />
  thrown in D:sunilbwebsiteprogsmagic_call.php on line 6</p>
<p>In the above program, when the script calls an undeclared variable $c-&gt;setName(), the <strong>magic method __call()</strong> is executed. On executing the <strong>magic method __call()</strong>, an exception is raised and the execution of the program stops there (unless we use the try..catch statements)</p>
<p><strong>Related Posts on PHP5 Tutorial &#8211; Object Oriented Programming (OOPS)</strong></p>
<ol>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-oops-tutorial-learn-to-create-a-php5-class" title="PHP5 Tutorial - Learn to create a PHP5 Class" target="_blank">PHP5 Tutorial &#8211; Learn to create a PHP5 Class</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-oops-tutorial-learn-to-creating-a-php5-class-object" title="PHP5 Tutorial - Learn to Create a PHP5 Class Object" target="_blank">PHP5 Tutorial &#8211; Learn to Create a PHP5 Class Object</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-oops-tutorial-defining-attributes-of-a-php5-class" title="PHP5 Tutorial - Defining Attributes of a PHP5 Class" target="_blank">PHP5 Tutorial &#8211; Defining Attributes of a PHP5 Class</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-oops-tutorial-defining-methods-of-a-php5-class" title="PHP5 Tutorial - Defining Methods of a PHP5 Class" target="_blank">PHP5 Tutorial &#8211; Defining Methods of a PHP5 Class</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-tutorial-creating-a-php5-constructor-__construct" title="PHP5 Tutorial - Creating a PHP5 Constructor __construct()" target="_blank">PHP5 Tutorial &#8211; Creating a PHP5 Constructor __construct()</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-tutorial-oops-creating-a-php5-destructor-__destruct" title="PHP5 Tutorial OOPS - Creating a PHP5 Destructor __destruct()" target="_blank">PHP5 Tutorial OOPS &#8211; Creating a PHP5 Destructor __destruct()</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-tutorial-oops-php5-class-access-specifiers-public-private-and-protected" title="PHP5 Tutorial OOPS - PHP5 Class Access Specifiers - public, private and protected" target="_blank">PHP5 Tutorial OOPS &#8211; PHP5 Class Access Specifiers &#8211; public, private and protected</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-tutorial-magic-methods-__tostring-method" title="PHP5 Tutorial - Magic Methods - __toString() method" target="_blank">PHP5 Tutorial &#8211; Magic Methods &#8211; __toString() method</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-oops-tutorial-__get-and-__set" title="PHP5 Tutorial __get() and __set()" target="_blank">PHP5 Tutorial &#8211; Magic Methods &#8211; __get() and __set()</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-oops-tutorial-magic-methods-__isset-and-__unset" title="PHP5 Tutorial - Magic Methods - __isset() and __unset()" target="_blank">PHP5 Tutorial &#8211; Magic Methods &#8211; __isset() and __unset()</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-oops-tutorial-magic-methods-__call-method" title="PHP5 Tutorial - Magic Methods - __call() method" target="_blank">PHP5 Tutorial &#8211; Magic Methods &#8211; __call() method</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-oops-tutorial-magic-methods-__autoload-method" title="PHP5 Tutorial - Magic Methods - __autoload() method" target="_blank">PHP5 Tutorial &#8211; Magic Methods &#8211; __autoload() method</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-oops-tutorial-magic-methods-__sleep-and-__wakeup" title="PHP5 Tutorial - Magic Methods - __sleep() and __wakeup()" target="_blank">PHP5 Tutorial &#8211; Magic Methods &#8211; __sleep() and __wakeup()</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-oops-tutorial-magic-methods-__clone-method" title="PHP5 Tutorial - Magic Methods - __clone() method" target="_blank">PHP5 Tutorial &#8211; Magic Methods &#8211; __clone() method</a></li>
</ol>

]]></content:encoded>
			<wfw:commentRss>http://www.sunilb.com/php/php5-oops-tutorial-magic-methods-__call-method/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>PHP5 Tutorial &#8211; Magic Methods &#8211; __isset() and __unset()</title>
		<link>http://www.sunilb.com/php/php5-oops-tutorial-magic-methods-__isset-and-__unset</link>
		<comments>http://www.sunilb.com/php/php5-oops-tutorial-magic-methods-__isset-and-__unset#comments</comments>
		<pubDate>Sat, 03 Nov 2007 07:30:22 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[PHP Tutorials]]></category>
		<category><![CDATA[PHP5 Magic Methods]]></category>
		<category><![CDATA[PHP5 OOPS Tutorials]]></category>

		<guid isPermaLink="false">http://www.sunilb.com/php/php-tutorials/php5-oops-tutorial-magic-methods-__isset-and-__unset</guid>
		<description><![CDATA[In my previous PHP5 OOPS tutorial on PHP5 Tutorial &#8211; Magic Methods &#8211; __get() and __set(), we learnt how and when to use these magic methods. This PHP5 OOPS tutorial will teach you how and when to use the magic methods __isset() and __unset(). These methods are automatically called internally when isset() and unset() is [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.sunilb.com%2Fphp%2Fphp5-oops-tutorial-magic-methods-__isset-and-__unset"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.sunilb.com%2Fphp%2Fphp5-oops-tutorial-magic-methods-__isset-and-__unset&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>In my previous <strong>PHP5 OOPS tutorial</strong> on <a href="http://www.sunilb.com/php/php-tutorials/php5-oops-tutorial-__get-and-__set" target="_blank">PHP5 Tutorial &#8211; Magic Methods &#8211; __get() and __set()</a>, we learnt how and when to use these <strong>magic methods</strong>.</p>
<p>This <strong>PHP5 OOPS tutorial</strong> will teach you how and when to use the <strong>magic methods __isset() and __unset()</strong>.</p>
<p><span id="more-47"></span></p>
<p><code><br />
<script type="text/javascript"><!--
google_ad_client = "pub-9205249129147978";
google_ad_width = 468;
google_ad_height = 60;
google_ad_format = "468x60_as";
google_ad_type = "text_image";
//2007-10-26: Sunilb.com 468x60
google_ad_channel = "7135663694";
google_color_border = "FFFFFF";
google_color_bg = "FFFFFF";
google_color_link = "0000FF";
google_color_text = "0F0F0F";
google_color_url = "CCCCCC";
//-->
</script><br />
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script><br />
</code></p>
<p>These methods are automatically called internally when isset() and unset() is called on undeclared data members. The <strong>magic method</strong> __isset() method receives an argument &#8211; the value of which is the name of the variable that the program wants to test if the variable is set or not.</p>
<p>The <strong>magic method</strong> __unset() method receives an argument &#8211; the value of which is the name of the variable that the program wants to unset.</p>
<p>Look at the example below:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Customer <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __set<span style="color: #009900;">&#40;</span><span style="color: #000088;">$dt</span><span style="color: #339933;">,</span> <span style="color: #000088;">$vl</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$dt</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$vl</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __get<span style="color: #009900;">&#40;</span><span style="color: #000088;">$dt</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$dt</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __isset<span style="color: #009900;">&#40;</span><span style="color: #000088;">$dt</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$dt</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __unset<span style="color: #009900;">&#40;</span><span style="color: #000088;">$dt</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$dt</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$c</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Customer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$c</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">name</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Sunil Bhatia&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$c</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$c</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><!-- google_ad_section_start --></p>
<p><code><br />
<script type="text/javascript"><!--
google_ad_client = "pub-9205249129147978";
google_ad_width = 468;
google_ad_height = 60;
google_ad_format = "468x60_as";
google_ad_type = "text_image";
//2007-10-26: Sunilb.com 468x60
google_ad_channel = "7135663694";
google_color_border = "FFFFFF";
google_color_bg = "FFFFFF";
google_color_link = "0000FF";
google_color_text = "0F0F0F";
google_color_url = "CCCCCC";
//-->
</script><br />
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script><br />
</code></p>
<blockquote><p>In the example above the script creates a new Customer Object. The program assigns a string value to an undeclared variable i.e. $c->name. The undeclared variable is handled by the magic method __set(). Read this post on <a href="http://www.sunilb.com/php/php-tutorials/php5-oops-tutorial-__get-and-__set" title="PHP5 Tutorial __get() and __set()" target="_blank">PHP5 Magic Methods __set()</a> if you need more understanding how the magic method __set() works.</p>
<p>The program ties to check if the undeclared variable i.e., $c->name has been set or not using the PHP method isset(). Since $c->name is an undeclared variable the PHP5 magic method __isset() is invoked that takes the name of the undeclared variable i.e. &#8220;name&#8221; and checks if the internal array $data["name"] is set or not.</p></blockquote>
<p>Similarly, when the program calls unset() on the undeclared variable i.e. $c->name, the PHP5 magic method __unset() is invoked that takes the name of the undeclared variable i.e. &#8220;name&#8221; and unsets the internal array $data["name"].</p>
<p><!-- google_ad_section_end --></p>
<p><strong>Related Posts on PHP5 Tutorial &#8211; Object Oriented Programming (OOPS)</strong></p>
<ol>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-oops-tutorial-learn-to-create-a-php5-class" title="PHP5 Tutorial - Learn to create a PHP5 Class" target="_blank">PHP5 Tutorial &#8211; Learn to create a PHP5 Class</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-oops-tutorial-learn-to-creating-a-php5-class-object" title="PHP5 Tutorial - Learn to Create a PHP5 Class Object" target="_blank">PHP5 Tutorial &#8211; Learn to Create a PHP5 Class Object</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-oops-tutorial-defining-attributes-of-a-php5-class" title="PHP5 Tutorial - Defining Attributes of a PHP5 Class" target="_blank">PHP5 Tutorial &#8211; Defining Attributes of a PHP5 Class</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-oops-tutorial-defining-methods-of-a-php5-class" title="PHP5 Tutorial - Defining Methods of a PHP5 Class" target="_blank">PHP5 Tutorial &#8211; Defining Methods of a PHP5 Class</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-tutorial-creating-a-php5-constructor-__construct" title="PHP5 Tutorial - Creating a PHP5 Constructor __construct()" target="_blank">PHP5 Tutorial &#8211; Creating a PHP5 Constructor __construct()</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-tutorial-oops-creating-a-php5-destructor-__destruct" title="PHP5 Tutorial OOPS - Creating a PHP5 Destructor __destruct()" target="_blank">PHP5 Tutorial OOPS &#8211; Creating a PHP5 Destructor __destruct()</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-tutorial-oops-php5-class-access-specifiers-public-private-and-protected" title="PHP5 Tutorial OOPS - PHP5 Class Access Specifiers - public, private and protected" target="_blank">PHP5 Tutorial OOPS &#8211; PHP5 Class Access Specifiers &#8211; public, private and protected</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-tutorial-magic-methods-__tostring-method" title="PHP5 Tutorial - Magic Methods - __toString() method" target="_blank">PHP5 Tutorial &#8211; Magic Methods &#8211; __toString() method</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-oops-tutorial-__get-and-__set" title="PHP5 Tutorial __get() and __set()" target="_blank">PHP5 Tutorial &#8211; Magic Methods &#8211; __get() and __set()</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-oops-tutorial-magic-methods-__isset-and-__unset" title="PHP5 Tutorial - Magic Methods - __isset() and __unset()" target="_blank">PHP5 Tutorial &#8211; Magic Methods &#8211; __isset() and __unset()</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-oops-tutorial-magic-methods-__call-method" title="PHP5 Tutorial - Magic Methods - __call() method" target="_blank">PHP5 Tutorial &#8211; Magic Methods &#8211; __call() method</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-oops-tutorial-magic-methods-__autoload-method" title="PHP5 Tutorial - Magic Methods - __autoload() method" target="_blank">PHP5 Tutorial &#8211; Magic Methods &#8211; __autoload() method</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-oops-tutorial-magic-methods-__sleep-and-__wakeup" title="PHP5 Tutorial - Magic Methods - __sleep() and __wakeup()" target="_blank">PHP5 Tutorial &#8211; Magic Methods &#8211; __sleep() and __wakeup()</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-oops-tutorial-magic-methods-__clone-method" title="PHP5 Tutorial - Magic Methods - __clone() method" target="_blank">PHP5 Tutorial &#8211; Magic Methods &#8211; __clone() method</a></li>
</ol>

]]></content:encoded>
			<wfw:commentRss>http://www.sunilb.com/php/php5-oops-tutorial-magic-methods-__isset-and-__unset/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>PHP5 OOPS Tutorial __get() and __set()</title>
		<link>http://www.sunilb.com/php/php5-oops-tutorial-__get-and-__set</link>
		<comments>http://www.sunilb.com/php/php5-oops-tutorial-__get-and-__set#comments</comments>
		<pubDate>Fri, 26 Oct 2007 16:32:51 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[PHP Tutorials]]></category>
		<category><![CDATA[PHP5 Magic Methods]]></category>
		<category><![CDATA[PHP5 OOPS Tutorials]]></category>

		<guid isPermaLink="false">http://www.sunilb.com/uncategorized/php5-oops-tutorial-__get-and-__set</guid>
		<description><![CDATA[This article talks about the use of __get() (double underscore &#8211; get()) and __set() (double underscore &#8211; set()) PHP5 OOPS magic methods. By default PHP is a Loosely typed language and therefore it is not necessary to declare variables before using them. This also holds true for using class members. Look at an example below. [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.sunilb.com%2Fphp%2Fphp5-oops-tutorial-__get-and-__set"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.sunilb.com%2Fphp%2Fphp5-oops-tutorial-__get-and-__set&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>This article talks about the use of <strong>__get()</strong> (double underscore &#8211; get()) and <strong>__set() </strong>(double underscore &#8211; set()) <strong>PHP5 OOPS</strong> magic methods.</p>
<p><code><br />
<script type="text/javascript"><!--
google_ad_client = "pub-9205249129147978";
google_ad_width = 468;
google_ad_height = 60;
google_ad_format = "468x60_as";
google_ad_type = "text_image";
//2007-10-26: Sunilb.com 468x60
google_ad_channel = "7135663694";
google_color_border = "FFFFFF";
google_color_bg = "E6E6E6";
google_color_link = "0000FF";
google_color_text = "0F0F0F";
google_color_url = "CCCCCC";
//-->
</script><br />
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script><br />
</code></p>
<p>By default <strong>PHP</strong> is a Loosely typed language and therefore it is not necessary to declare variables before using them. This also holds true for using class members. Look at an example below.</p>
<p><span id="more-38"></span></p>
<blockquote><p>&lt;?php</p>
<p>class Customer {<br />
public $name;<br />
}</p>
<p>$c = new Customer();<br />
$c-&gt;name = &#8220;Sunil&#8221;; // $name is set because its public</p>
<p>$c-&gt;email = &#8220;email@domain.com&#8221;; //assigning email@domain.com to the $email variable.</p>
<p>?&gt;</p></blockquote>
<p>Ideally in a strict language this would have been an error. But, with <strong>PHP</strong> this works perfectly well as you can assign values to an undefined variable.</p>
<p>Because of the above limitation, <strong>PHP</strong> engine provides two magic methods <strong>__get()</strong> and <strong>__set()</strong>. <strong>__get()</strong> is used when value from an undefined variable is to be read and <strong>__set()</strong> is used when a value is to be assigned to a undefined variable of a class.</p>
<p><strong>__set()</strong> allows you to provide functionality to validate data being stored. See example below:</p>
<blockquote><p>&lt;?php</p>
<p>class Customer {<br />
public $name;<br />
private $data = array();</p>
<p>public function __set($dt, $vl) {<br />
$this-&gt;data[$dt] = $vl;<br />
}</p>
<p>public function __get($dt) {<br />
return $this-&gt;data[$dt];<br />
}<br />
}</p>
<p>$c = new Customer();<br />
$c-&gt;name = &#8220;Sunil&#8221;; // $name is set because its public</p>
<p>$c-&gt;email = &#8220;email@domain.com&#8221;; //assigning email@domain.com to the $email variable.</p>
<p>echo $c-&gt;email;</p>
<p>?&gt;</p></blockquote>
<p>In the above example when email@domain.com is assigned to the undefined variable <strong><em>$email</em></strong>, the magic method <strong>__set()</strong> is called. To this <strong>__set()</strong> method the name of the variable is passed into <em><strong>$dt</strong></em> variable of __set() method and the value i.e. email@domain.com is passed to <em><strong>$vl</strong></em> variable of the __set() method.</p>
<p><code><br />
<script type="text/javascript"><!--
google_ad_client = "pub-9205249129147978";
google_ad_width = 468;
google_ad_height = 60;
google_ad_format = "468x60_as";
google_ad_type = "text_image";
//2007-10-26: Sunilb.com 468x60
google_ad_channel = "7135663694";
google_color_border = "FFFFFF";
google_color_bg = "E6E6E6";
google_color_link = "0000FF";
google_color_text = "0F0F0F";
google_color_url = "CCCCCC";
//-->
</script><br />
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script><br />
</code></p>
<p>The next step is to store these values into the <em><strong>$data</strong></em> array so that you could retrieve it later.</p>
<p>The <strong>__get()</strong> method works in the similar fashion. When you echo $c-&gt;email, __get() method is called and the name email is passed in the <em><strong>$dt</strong></em> of the __get() method.</p>
<p><strong>Tip:</strong><br />
It is possible to  stop this behavior of PHP to assign values to undefined issues. The solution is that you raise an exception from within __set() method. Look at the code below:</p>
<blockquote><p>&lt;?</p>
<p>class Customer {</p>
<p>private $name;</p>
<p>public function __set($dt, $vl) {<br />
throw new Exception(&#8220;Cannot assign values to undefined variables&#8221;,1);<br />
}</p>
<p>}</p>
<p>$c = new Customer();<br />
$c-&gt;email = &#8220;email@domain.com&#8221;; //this will cause an exception to be raised</p>
<p>?&gt;</p></blockquote>
<p><strong>Related Articles:</strong><br />
<a href="http://www.sunilb.com/php/php-tutorials/php5-tutorial-magic-methods-__tostring-method">PHP5 OOPS &#8211; Magic Methods __toString()</a><br />
<a href="http://www.sunilb.com/php/php-tutorials/tutorial-1-introduction-to-php-web-programming">PHP5 OOPS Tutorial</a></p>
<p align="center"><!--subscribe2--></p>

]]></content:encoded>
			<wfw:commentRss>http://www.sunilb.com/php/php5-oops-tutorial-__get-and-__set/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>PHP5 Tutorial &#8211; Magic Methods &#8211; __toString() method</title>
		<link>http://www.sunilb.com/php/php5-tutorial-magic-methods-__tostring-method</link>
		<comments>http://www.sunilb.com/php/php5-tutorial-magic-methods-__tostring-method#comments</comments>
		<pubDate>Thu, 25 Oct 2007 17:54:35 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[PHP Tutorials]]></category>
		<category><![CDATA[PHP5 Magic Methods]]></category>
		<category><![CDATA[PHP5 OOPS Tutorials]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.sunilb.com/php/php-tutorials/php5-tutorial-magic-methods-__tostring-method</guid>
		<description><![CDATA[PHP5 provides a magic method by the name of __toString() (double underscore followed by toString()) which is useful for debugging purposes. The __toString() method is automatically called when an object in PHP5 is converted into a string for the purpose of display or concatenation. Following is the example of the __toString() method: &#60;?php class Customer [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.sunilb.com%2Fphp%2Fphp5-tutorial-magic-methods-__tostring-method"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.sunilb.com%2Fphp%2Fphp5-tutorial-magic-methods-__tostring-method&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>PHP5 provides a magic method by the name of __toString() (double underscore followed by toString()) which is useful for debugging purposes.</p>
<p>The __toString() method is automatically called when an object in PHP5 is converted into a string for the purpose of display or concatenation.</p>
<p><span id="more-34"></span></p>
<p><code><br />
<script type="text/javascript">
<!--
google_ad_client = "pub-9205249129147978"; google_ad_width = 300; google_ad_height = 250; google_ad_format = "300x250_as"; google_ad_type = "text_image";
//2007-10-18: SB - Square - Text/Image
google_ad_channel = "3552230476"; google_color_border = "FFFFFF"; google_color_bg = "FFFFFF"; google_color_link = "0000FF"; google_color_text = "0F0F0F"; google_color_url = "CCCCCC";
//-->
</script><br />
<script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"> </script><br />
</code></p>
<p>Following is the example of the __toString() method:</p>
<blockquote><p>&lt;?php</p>
<p>class Customer {<br />
private $firstName, $lastName, $email;</p>
<p>public function __construct($firstName, $lastName, $email) {<br />
$this-&gt;firstName = $firstName;<br />
$this-&gt;lastName = $lastName;<br />
$this-&gt;email = $email;<br />
}</p>
<p>public function __toString() {<br />
return &#8220;Debug message from Customer Class : First Name = &#8221; . $this-&gt;firstName . &#8220;, Last Name = &#8221; . $this-&gt;lastName . &#8220;, Email = &#8221; . $this-&gt;email;<br />
}<br />
}</p>
<p>$c = new Customer(&#8220;Sunil&#8221;,&#8221;Bhatia&#8221;,&#8221;email@domain.com&#8221;);</p>
<p>echo &#8220;Customer Object is &gt;&gt;&#8221; . $c;</p>
<p>?&gt;</p></blockquote>
<p><strong>Output:</strong></p>
<blockquote><p>Customer Object is &gt;&gt; Debug message from Customer Class : First Name = Sunil, Last Name = Bhatia, Email = email@domain.com</p></blockquote>
<p>See how in this example $c Customer Object got converted into a string type when used with the dot (.) concatenation operator. In the background the magic method __toString() is automatically called when such a conversion happens.</p>
<p>Security Tip:<br />
Be careful not to include sensitive data as part of the output as you could compromise security by leaking secure information. Many applications are written to write object states in a log file, therefore you should ensure that sensitive information like Credit Card information, etc is not made available through the magic method __toString()</p>
<p>Subscribe to my newsletter and be informed as a new PHP5 tutorial is posted online:</p>
<p align="center"> <!--subscribe2--></p>
<p><code><br />
<script type="text/javascript">
<!--
google_ad_client = "pub-9205249129147978"; google_ad_width = 300; google_ad_height = 250; google_ad_format = "300x250_as"; google_ad_type = "text_image";
//2007-10-18: SB - Square - Text/Image
google_ad_channel = "3552230476"; google_color_border = "FFFFFF"; google_color_bg = "FFFFFF"; google_color_link = "0000FF"; google_color_text = "0F0F0F"; google_color_url = "CCCCCC";
//-->
</script><br />
<script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"> </script><br />
</code></p>

]]></content:encoded>
			<wfw:commentRss>http://www.sunilb.com/php/php5-tutorial-magic-methods-__tostring-method/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

