<?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 Tutorial</title>
	<atom:link href="http://www.sunilb.com/tag/php5-tutorial/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 Tutorials &#8211; Abstract Class and Interface</title>
		<link>http://www.sunilb.com/php/php5-tutorials-abstract-class-and-interface</link>
		<comments>http://www.sunilb.com/php/php5-tutorials-abstract-class-and-interface#comments</comments>
		<pubDate>Sun, 03 Feb 2008 08:26:29 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[PHP5 OOPS Tutorials]]></category>
		<category><![CDATA[PHP5]]></category>
		<category><![CDATA[PHP5 OOPS]]></category>
		<category><![CDATA[PHP5 Tutorial]]></category>

		<guid isPermaLink="false">http://www.sunilb.com/php/php5-tutorials-abstract-class-and-interface</guid>
		<description><![CDATA[In this post, we learn what is an abstract class and an interface. What is an Abstract Class? Private methods cannot be Abstract What is an Interface? Defining an Interface Abstract Class v/s Interface What is an Abstract Class? An abstract class is a class with or without data members that provides some functionality and [...]]]></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-tutorials-abstract-class-and-interface"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.sunilb.com%2Fphp%2Fphp5-tutorials-abstract-class-and-interface&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>In this post, we learn what is an abstract class and an interface.</p>
<ul>
<li>What is an Abstract Class?</li>
<li>Private methods cannot be Abstract</li>
<li>What is an Interface?</li>
<li>Defining an Interface</li>
<li>Abstract Class v/s Interface</li>
</ul>
<p><span id="more-77"></span></p>
<h3>What is an Abstract Class?</h3>
<p>An abstract class is a class with or without data members that provides some functionality and leaves the remaining functionality for its child class to implement. The child class must provide the functionality not provided by the abstract class or else the child class also becomes abstract.</p>
<p>Objects of an abstract and interface class cannot be created i.e. only objects of concrete class can be created</p>
<p>To define a class as Abstract, the keyword <i>abstract</i> is to be used e.g. abstract class ClassName { }</p>
<p><strong>Example of Abstract Class</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&nbsp;
abstract <span style="color: #000000; font-weight: bold;">class</span> Furniture <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$height</span><span style="color: #339933;">,</span> width<span style="color: #339933;">,</span> length<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setData<span style="color: #009900;">&#40;</span><span style="color: #000088;">$h</span><span style="color: #339933;">,</span> <span style="color: #000088;">$w</span><span style="color: #339933;">,</span> <span style="color: #000088;">$l</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;">height</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$h</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">width</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$w</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">length</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$l</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">//this function is declared as abstract and hence the function</span>
        <span style="color: #666666; font-style: italic;">//body will have to be provided in the child class</span>
	<span style="color: #000000; font-weight: bold;">public</span> abstract <span style="color: #000000; font-weight: bold;">function</span> getPrice<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> BookShelf <span style="color: #000000; font-weight: bold;">extends</span> Furniture <span style="color: #009900;">&#123;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$price</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">public</span> setData<span style="color: #009900;">&#40;</span><span style="color: #000088;">$h</span><span style="color: #339933;">,</span> <span style="color: #000088;">$w</span><span style="color: #339933;">,</span> <span style="color: #000088;">$l</span><span style="color: #339933;">,</span> <span style="color: #000088;">$p</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      parent<span style="color: #339933;">::</span><span style="color: #004000;">setData</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$h</span><span style="color: #339933;">,</span> <span style="color: #000088;">$w</span><span style="color: #339933;">,</span> <span style="color: #000088;">$l</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">price</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$p</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
   <span style="color: #666666; font-style: italic;">//this is the function body of the parent abstract method</span>
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getPrice<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;">price</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<blockquote><p>In the above example, the method getPrice() in class Furniture has been declared as Abstract. This means that its the responsibility of the child class to provide the functionality of getPrice(). The BookShelf class is a child of the Furniture class and hence provides the function body for getPrice().</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>
<h3>Private methods cannot be abstract</h3>
<p>If a method is defined as abstract then it cannot be declared as private (it can only be public or protected). This is because a private method cannot be inherited.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">abstract <span style="color: #000000; font-weight: bold;">class</span> BaseClass <span style="color: #009900;">&#123;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">private</span> abstract <span style="color: #000000; font-weight: bold;">function</span> myFun<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> DerivedClass <span style="color: #000000; font-weight: bold;">extends</span> BaseClass <span style="color: #009900;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> myFun<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	   <span style="color: #666666; font-style: italic;">//logic here</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$d</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DerivedClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//will cause error</span></pre></div></div>

<h3>What is an Interface?</h3>
<p>An interface is a contract between unrelated objects to perform a common function. An interface enables you to specify that an object is capable of performing a certain function, but it does not necessarily tell you how the object does so, this means that it leaves for classes implementing an interface to define its behaviour.</p>
<p>To extend from an Interface, keyword <i>implements</i> is used.</p>
<blockquote><p>We can have a class extend from more than one Interface.</p></blockquote>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #000000; font-weight: bold;">interface</span> Storable <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">function</span> getContentsAsText<span style="color: #009900;">&#40;</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;">class</span> Document implements Storable <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getContentsAsText<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: #0000ff;">&quot;This is Text of the Document<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: #000000; font-weight: bold;">class</span> Indexer <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> readAndIndex<span style="color: #009900;">&#40;</span>Storable <span style="color: #000088;">$s</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$textData</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$s</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getContentsAsText</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">//do necessary logic to index</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$textData</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$p</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Document<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Indexer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$i</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">readAndIndex</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$p</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<blockquote><p>In the above example, Document and the Indexer class are two independant classes. The Indexer class is designed to index the contents of any text. Using the Storable interface above, we declare a method getContentsAsText() in the Document class. Because the Indexer class is only concerned with the TEXT, hence we can call getContentsAsText() method on the object of Document. This way any class if it implements the method getContentsAsText() can get indexed</p></blockquote>
<h3>Difference between Abstract Class and Interface</h3>
<p><strong>Abstract Classes</strong></p>
<ol>
<li>An abstract class can provide some functionality and leave the rest for derived class</li>
<li>The derived class may or may not override the concrete functions defined in base class</li>
<li>The child class extended from an abstract class should logically be related</li>
</ol>
<p><strong>Interface</strong></p>
<ol>
<li>An interface cannot contain any functionality. It only contains definitions of the methods</li>
<li>The derived class must provide code for all the methods defined in the interface</li>
<li>Completely different and non-related classes can be logically be grouped together using an interface</li>
<p>Subscribe below for article updates</p>
<p><!--subscribe2--></p>
</ol>
<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-this-variable-explained" title="PHP5 Tutorial - $this variable explained">PHP5 Tutorial &#8211; $this variable explained</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-tutorial-instanceof-operator-explained" title="PHP5 Tutorial - instanceOf Operator Explained">PHP5 Tutorial &#8211; instanceOf Operator Explained</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-tutorial-defining-class-constants" title="PHP5 Tutorial - instanceOf Operator Explained">PHP5 Tutorial &#8211; Defining Class Constants</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-tutorial-inheritance-in-php5" title="PHP5 Tutorial - Inheritance">PHP5 Tutorial &#8211; Inheritance</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>
<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>

]]></content:encoded>
			<wfw:commentRss>http://www.sunilb.com/php/php5-tutorials-abstract-class-and-interface/feed</wfw:commentRss>
		<slash:comments>7</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>
	</channel>
</rss>

