<?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; Magic Methods</title>
	<atom:link href="http://www.sunilb.com/tag/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; __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>
	</channel>
</rss>

