<?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; PHP</title>
	<atom:link href="http://www.sunilb.com/category/php/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>Writing Website Scrapers in PHP</title>
		<link>http://www.sunilb.com/php/writing-website-scrapers-in-php</link>
		<comments>http://www.sunilb.com/php/writing-website-scrapers-in-php#comments</comments>
		<pubDate>Tue, 26 Feb 2008 15:35:01 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[PHP Code Examples]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[web programming]]></category>

		<guid isPermaLink="false">http://www.sunilb.com/php/writing-website-scrapers-in-php</guid>
		<description><![CDATA[This article discusses about how to write a website scraper using PHP for web site data extraction. The concepts taught can be applied and programmed in Java, C#, etc. Basically any language that has a powerful string processing capability. This article will teach you the basics of website scraping. The article will further cover a [...]]]></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%2Fwriting-website-scrapers-in-php"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.sunilb.com%2Fphp%2Fwriting-website-scrapers-in-php&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>This article discusses about how to write a website scraper using PHP for web site data extraction. The concepts taught can be applied and programmed in Java, C#, etc. Basically any language that has a powerful string processing capability. This article will teach you the basics of website scraping. The article will further cover a tutorial to find web ranking from Yahoo.com search engine.</p>
<p><span id="more-84"></span></p>
<h3>Steps involved to write a scraping program</h3>
<ol>
<li>Visit the URL</li>
<li>Understand the pattern</li>
<li>Validate the structure of pattern on different URLs</li>
<li>Write the program</li>
<li>Test the program using various input parameters</li>
</ol>
<p>Lets visit each of these steps one bit at a time.</p>
<h3>1. Visit the URL</h3>
<p>For this tutorial, we will extract Yahoo&#8217;s &#8220;Today&#8217;s Top Searches&#8221; section towards the end of their home page (http://www.yahoo.com/).</p>
<h3>2. Understand the pattern</h3>
<p>Before you begin to write a web scraping program, its important to understand the pattern of the data that you wish to extract. View the page source to understand the pattern. Mentioned below.</p>
<p>The string of text that we should parse is given below:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&nbsp;
&lt;div id=&quot;popsearchbd&quot; class=&quot;bd&quot;&gt;
&lt;ol start=1&gt;&lt;li&gt;&lt;a href=&quot;r/dy/*-http://search.yahoo.com/search?p=Heidi+Klum&amp;cs=bz&amp;fr=fp-buzzmod&quot;&gt;Heidi Klum&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;r/dy/*-http://search.yahoo.com/search?p=Sarah+Larson&amp;cs=bz&amp;fr=fp-buzzmod&quot;&gt;Sarah Larson&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;r/dy/*-http://search.yahoo.com/search?p=Oscar+Videos&amp;cs=bz&amp;fr=fp-buzzmod&quot;&gt;Oscar Videos&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;r/dy/*-http://search.yahoo.com/search?p=Brad+Renfro&amp;cs=bz&amp;fr=fp-buzzmod&quot;&gt;Brad Renfro&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;r/dy/*-http://search.yahoo.com/search?p=Gary+Busey&amp;cs=bz&amp;fr=fp-buzzmod&quot;&gt;Gary Busey&lt;/a&gt;&lt;/li&gt;&lt;/ol&gt;&lt;ol start=6&gt;&lt;li&gt;&lt;a href=&quot;r/dy/*-http://search.yahoo.com/search?p=Barack+Obama&amp;cs=bz&amp;fr=fp-buzzmod&quot;&gt;Barack Obama&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;r/dy/*-http://search.yahoo.com/search?p=Razzie+Awards&amp;cs=bz&amp;fr=fp-buzzmod&quot;&gt;Razzie Awards&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;r/dy/*-http://search.yahoo.com/search?p=Raisin+in+the+Sun&amp;cs=bz&amp;fr=fp-buzzmod&quot;&gt;Raisin in the Sun&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;r/dy/*-http://search.yahoo.com/search?p=Stay+Home+Moms&amp;cs=bz&amp;fr=fp-buzzmod&quot;&gt;Stay Home Moms&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;r/dy/*-http://search.yahoo.com/search?p=Net+Neutrality&amp;cs=bz&amp;fr=fp-buzzmod&quot;&gt;Net Neutrality&lt;/a&gt;&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;
&lt;/div&gt;</pre></div></div>

<p>The pattern is that each Search Phrase is enclosed within a &lt;li&gt;&lt;a&gt;&lt;/a&gt;&lt;/li&gt; tag. Therefore, we should parse everything between &lt;a&gt;&lt;/a&gt; of this text piece to get the text.</p>
<p><center><br />
<code><br />
<script type="text/javascript"><!--
google_ad_client = "pub-9205249129147978";
/* SB - PHP Scraper */
google_ad_slot = "3523700425";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script><br />
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></code><br />
</center></p>
<h3>3. Validate the structure of pattern on different URLs</h3>
<p>If you are writing a script to fetch data that has pagination, you should remember to validate the structure on 3 &#8211; 4 pages before you start developing code. The reason behind doing this is that the presentation of the first page could differ in subsequent pages.</p>
<h3>4. Write the program</h3>
<p>You could use any programming language like Java, C#, PHP, PERL, etc. for this processing. I have used PHP for this example.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #666666; font-style: italic;">//change fso() to f sock open (my blog was causing an error)</span>
<span style="color: #666666; font-style: italic;">//change fwt() to f write (my blog was causing an error)</span>
<span style="color: #666666; font-style: italic;">//change fgs() to f gets (my blog was causing an error)</span>
<span style="color: #666666; font-style: italic;">//change fc() to f close (my blog was causing an error)</span>
&nbsp;
<span style="color: #000088;">$fp</span> <span style="color: #339933;">=</span> fso<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;www.yahoo.com&quot;</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">80</span><span style="color: #339933;">,</span> <span style="color: #000088;">$errno</span><span style="color: #339933;">,</span> <span style="color: #000088;">$errstr</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">30</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$fp</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$errstr</span> (<span style="color: #006699; font-weight: bold;">$errno</span>)&lt;br /&gt;<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: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$out</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;GET / HTTP/1.1<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$out</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;Host: www.yahoo.com<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$out</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.12) Gecko/20080201 Firefox/2.0.0.12<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$out</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;Connection: Close<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000088;">$str</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
	fwt<span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #339933;">,</span> <span style="color: #000088;">$out</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">feof</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$str</span> <span style="color: #339933;">.=</span> fgs<span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1024</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	fc<span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$pos</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strpos</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;&lt;div id=<span style="color: #000099; font-weight: bold;">\&quot;</span>popsearchbd<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$pos</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$pos</span> <span style="color: #339933;">+</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&lt;div id=<span style="color: #000099; font-weight: bold;">\&quot;</span>popsearchbd<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pos</span> <span style="color: #339933;">==</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;No information available&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$pos</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strpos</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;fp-buzzmod<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$pos</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pos</span> <span style="color: #339933;">===</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000088;">$pos</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$pos</span> <span style="color: #339933;">+</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;fp-buzzmod<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$temppos</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$pos</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$pos</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strpos</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;&lt;/a&gt;&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$pos</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000088;">$datalength</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$pos</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$temppos</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str</span><span style="color: #339933;">,</span> <span style="color: #000088;">$temppos</span> <span style="color: #339933;">,</span> <span style="color: #000088;">$datalength</span><span style="color: #009900;">&#41;</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: #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>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<h3>5. Test the program using various input parameters</h3>
<p>You should test your program for all the parameters that the web page can take. I have experienced change in layout &#038; data based on the parameters that are passed.</p>
<h3>Notes on processing forms and cookies</h3>
<p>Some pages use form data and cookies to render data. In such cases you should remember to check the Request and Response headers and identify what is necessary to get the results that you want. If the page requires a cookie value, you should then use the cookie information in your Request Headers. Look at the note below that I use to inspect Request and Response headers.</p>
<p><center><br />
<code><br />
<script type="text/javascript"><!--
google_ad_client = "pub-9205249129147978";
/* SB - PHP Scraper Bottom */
google_ad_slot = "6345077142";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script><br />
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script><br />
</code><br />
</center></p>
<h3>Tool to inspect Request and Response Headers</h3>
<p>I use Live HTTP Headers (a plug in for FireFox) to check for Request and Response Headers. Visit http://livehttpheaders.mozdev.org for more details. To install this plug in visit http://livehttpheaders.mozdev.org/installation.html and click on the &#8216;download it&#8217; link on the latest release. Please read the release notes before installing a particular version.</p>
<p>Please feel free to use the comments section down to share other tools that you guys use to monitor Request and Response Headers.</p>
<h3>Future Maintenance of the program</h3>
<p>From a maintenance perspective you should monitor the page frequently and re-validate the HTML structure. The reason for this is because nothing in this world is constant and so is the website. Design changes could result in a change in the HTML code. I recommend scheduling this activity for at least once a month.</p>
<p>This is all that is there in this tutorial. In the next tutorial, I will guide you step-by-step on how to create a program to check for yahoo.com search engine ranking. Use the subscribe form below to keep yourself updated when the tutorial goes live.</p>
<h5>Would you like to check out some of my <a href="http://www.sunilb.com/php-tutorial"><b>PHP5 Tutorials</b></a> as well?</h5>
<p><!--subscribe2--></p>

]]></content:encoded>
			<wfw:commentRss>http://www.sunilb.com/php/writing-website-scrapers-in-php/feed</wfw:commentRss>
		<slash:comments>25</slash:comments>
		</item>
		<item>
		<title>PHP 5 Tutorial &#8211; Handling Exceptions in PHP5</title>
		<link>http://www.sunilb.com/php/php-5-tutorial-handling-exceptions-in-php5</link>
		<comments>http://www.sunilb.com/php/php-5-tutorial-handling-exceptions-in-php5#comments</comments>
		<pubDate>Thu, 14 Feb 2008 08:58:21 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[PHP Class Examples]]></category>
		<category><![CDATA[PHP Code Examples]]></category>
		<category><![CDATA[PHP Tutorials]]></category>
		<category><![CDATA[PHP5 OOPS Tutorials]]></category>
		<category><![CDATA[PHP5]]></category>
		<category><![CDATA[PHP5 Exceptions]]></category>
		<category><![CDATA[PHP5 OOPS]]></category>
		<category><![CDATA[PHP5 Tutorials]]></category>

		<guid isPermaLink="false">http://www.sunilb.com/php/php-tutorials/php-5-tutorial-handling-exceptions-in-php5</guid>
		<description><![CDATA[In this tutorial we will cover the following: What is an exception? The use of a try&#8230;catch block Anatomy of PHP5 Exception class Extending the Exception class A note on unhanded exceptions What is an exception? An exception is a logical/system error that occurs during the normal execution of a script. The exception could either [...]]]></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%2Fphp-5-tutorial-handling-exceptions-in-php5"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.sunilb.com%2Fphp%2Fphp-5-tutorial-handling-exceptions-in-php5&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>In this tutorial we will cover the following:</p>
<ul>
<li>What is an exception?</li>
<li>The use of a try&#8230;catch block</li>
<li>Anatomy of PHP5 Exception class</li>
<li>Extending the Exception class</li>
<li>A note on unhanded exceptions</li>
</ul>
<p><span id="more-81"></span></p>
<h3>What is an exception?</h3>
<p>An exception is a logical/system error that occurs during the normal execution of a script. The exception could either be raised by the system or the program itself it the exception cannot be handled and the caller script/function needs to be informed about the same.</p>
<h3>The use of a try&#8230;catch block</h3>
<p>PHP5 introduces the try&#8230;catch block to trap exceptions. Look at the example below.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&nbsp;
try <span style="color: #009900;">&#123;</span>
   check<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
catch<span style="color: #009900;">&#40;</span>Exception <span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Message : &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$e</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Code : &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$e</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getCode</span><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;">function</span> check<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>some error condition<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;Error String&quot;</span><span style="color: #339933;">,</span>Error Code<span style="color: #009900;">&#41;</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 check() is called between the try {} block. The try{} block is the area where you will place your code that could raise an exception. Below the try{} block is the catch() {} block. The catch block expects the Exception type of object as a parameter. Within the catch() {} block you will place your logic to either fix the issue or log the error.</p>
<p>In the function check(), we raise an Exception using the &#8216;throw&#8217; keyword. The statement following &#8216;throw&#8217; is the syntax of creating a new object of Exception type. The exception class accepts two parameters. The left parameter is a string that is the error message and the right parameter is the integer error code that you wish to assign to that error.</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>Anatomy of PHP5 Exception class</h3>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Exception <span style="color: #009900;">&#123;</span>
	protected <span style="color: #000088;">$message</span><span style="color: #339933;">;</span>
	protected <span style="color: #000088;">$code</span><span style="color: #339933;">;</span>
	protected <span style="color: #000088;">$file</span><span style="color: #339933;">;</span>
	protected <span style="color: #000088;">$line</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$string</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$trace</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> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$message</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #000088;">$code</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __toString<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	final <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getCode<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	final <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getMessage<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	final <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getFile<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	final <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getLine<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	final <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getTrace<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	final <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getTraceAsString<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	final <span style="color: #000000; font-weight: bold;">private</span> __clone<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<blockquote><p>In the above example, except for __construct and __toString(), no other method can be overridden as all other methods are &#8216;final&#8217;.</p>
<p><b>Related Reading:</b></p>
<ul>
<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-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-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/php-5-tutorial-final-class-and-methods" title="PHP 5 Tutorial - Final Class and Methods" target="_blank">PHP 5 Tutorial &#8211; Final Class and Methods</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>
</ul>
</blockquote>
<h3>Extending the Exception class</h3>
<p>You can also extend the exception class as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> CustomerException <span style="color: #000000; font-weight: bold;">extends</span> Exception <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> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$message</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #000088;">$code</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000088;">$t_message</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Exception raised in CustomerException &quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$t_message</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;with message : &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$message</span><span style="color: #339933;">;</span>
&nbsp;
    parent<span style="color: #339933;">::</span>__construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$t_message</span><span style="color: #339933;">,</span> <span style="color: #000088;">$code</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> testException<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	throw <span style="color: #000000; font-weight: bold;">new</span> CustomerException<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;CustomerException has been raised&quot;</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">101</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
try <span style="color: #009900;">&#123;</span>
	testException<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
catch<span style="color: #009900;">&#40;</span>CustomerException <span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Error Message : &quot;</span> <span style="color: #000088;">$e</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Error Code : &quot;</span> <span style="color: #000088;">$e</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getCode</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>Output:</strong></p>
<p>Error Message : CustomerException has been raised<br />
Error Code : 101</p>
<p><strong>Related Reading:</strong><br />
<a href="http://www.sunilb.com/php/php-tutorials/php5-tutorial-inheritance-in-php5" title="PHP5 Tutorial - Inheritance">PHP5 Tutorial &#8211; Inheritance</a></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>
<h3>A note on unhanded exceptions</h3>
<p>All un-handled exceptions are passed up the function stack order till the time either a try&#8230;catch block is not made available. If a try&#8230;catch block is unavailable it is passed to the PHP core and the program execution stops there.</p>
<p>Please feel free to write back for any clarification.</p>
<p><strong>Subscribe to my free newsletter:</strong></p>
<p><!--subscribe2--></p>
</ol>
<p><strong>Related Posts on <b>PHP5 Tutorial</b> &#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/php5-tutorials-abstract-class-and-interface" title="PHP5 Tutorials - Abstract Class and Interface">PHP5 Tutorials &#8211; Abstract Class and Interface</a></li>
<li><a href="http://www.sunilb.com/php/php5-oops-tutorials-polymorphism" title="PHP5 OOPS Tutorials - Polymorphism">PHP5 OOPS Tutorials &#8211; Polymorphism</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php-5-tutorials-static-data-members-and-methods" title="PHP 5 Tutorials - Static Data Members and Methods">PHP 5 Tutorials &#8211; Static Data Members and Methods</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php-5-tutorial-final-class-and-methods" title="PHP 5 Tutorial - Final Class and Methods" target="_blank">PHP 5 Tutorial &#8211; Final Class and Methods</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/php-5-tutorial-handling-exceptions-in-php5/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>PHP 5 Tutorial &#8211; Final Class and Methods</title>
		<link>http://www.sunilb.com/php/php-5-tutorial-final-class-and-methods</link>
		<comments>http://www.sunilb.com/php/php-5-tutorial-final-class-and-methods#comments</comments>
		<pubDate>Thu, 14 Feb 2008 06:41:17 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[PHP Class Examples]]></category>
		<category><![CDATA[PHP Tutorials]]></category>
		<category><![CDATA[PHP5 OOPS Tutorials]]></category>
		<category><![CDATA[PHP5]]></category>
		<category><![CDATA[PHP5 OOPS]]></category>
		<category><![CDATA[PHP5 Tutorials]]></category>

		<guid isPermaLink="false">http://www.sunilb.com/php/php-tutorials/php-5-tutorial-final-class-and-methods</guid>
		<description><![CDATA[In this PHP tutorial we will understand the following: Meaning of Final Class Meaning of Final Method When to declare a class as final When to declare a method as final Meaning of Final Class A final class is a class that cannot be extended. To declare a class as final, you need to prefix [...]]]></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%2Fphp-5-tutorial-final-class-and-methods"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.sunilb.com%2Fphp%2Fphp-5-tutorial-final-class-and-methods&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>In this PHP tutorial we will understand the following:</p>
<ul>
<li>Meaning of Final Class</li>
<li>Meaning of Final Method</li>
<li>When to declare a class as final</li>
<li>When to declare a method as final</li>
</ul>
<p><span id="more-80"></span></p>
<h3>Meaning of Final Class</h3>
<p>A final class is a class that cannot be extended. To declare a class as final, you need to prefix the &#8216;class&#8217; keyword with &#8216;final&#8217;. Example below.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&nbsp;
final <span style="color: #000000; font-weight: bold;">class</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> myMethod<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;BaseClass method called&quot;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//this will cause Compile error</span>
<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> myMethod<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;DerivedClass method called&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> DerivedClass<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;">myMethod</span><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 example, BaseClass is declared as final and hence cannot be extended (inherited). DerivedClass tries to extend from BaseClass and hence the compiler will throw a compile error.
</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>Meaning of Final Method</h3>
<p>A final method is a method that cannot be overridden. To declare a method as final, you need to prefix the function name with the &#8216;final&#8217; keyword. Example below:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> BaseClass <span style="color: #009900;">&#123;</span>
   final <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> myMethod<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;BaseClass method called&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> DerivedClass <span style="color: #000000; font-weight: bold;">extends</span> BaseClass <span style="color: #009900;">&#123;</span>
   <span style="color: #666666; font-style: italic;">//this will cause Compile error</span>
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> myMethod<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;DerivedClass method called&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> DerivedClass<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;">myMethod</span><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 example, DerivedClass extends from BaseClass. BaseClass has the method myMethod() declared as final and this cannot be overridden. In this case the compiler causes a compile error.
</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>When to declare a class as final</h3>
<p>You should declare a class as final when you think that you implementation of that class should not change in the derived class. You should do this mainly for Utility classes where you don&#8217;t want the behavior/implementation of your class to change.</p>
<h3>When to declare a method as final</h3>
<p>You should declare a class method as final when you think that the method you develop contains necessary functionality to support your application and any modification or change to the functionality can cause unexpected errors/bugs.</p>
<p>Please feel free to write back for any clarification.</p>
<p><strong>Subscribe to my free newsletter:</strong></p>
<p><!--subscribe2--></p>
</ol>
<p><strong>Related Posts on <b>PHP5 Tutorial</b> &#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/php5-tutorials-abstract-class-and-interface" title="PHP5 Tutorials - Abstract Class and Interface">PHP5 Tutorials &#8211; Abstract Class and Interface</a></li>
<li><a href="http://www.sunilb.com/php/php5-oops-tutorials-polymorphism" title="PHP5 OOPS Tutorials - Polymorphism">PHP5 OOPS Tutorials &#8211; Polymorphism</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php-5-tutorials-static-data-members-and-methods" title="PHP 5 Tutorials - Static Data Members and Methods">PHP 5 Tutorials &#8211; Static Data Members and Methods</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/php-5-tutorial-final-class-and-methods/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>PHP 5 Tutorials &#8211; Static Data Members and Methods</title>
		<link>http://www.sunilb.com/php/php-5-tutorials-static-data-members-and-methods</link>
		<comments>http://www.sunilb.com/php/php-5-tutorials-static-data-members-and-methods#comments</comments>
		<pubDate>Tue, 12 Feb 2008 09:47:49 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[PHP Class Examples]]></category>
		<category><![CDATA[PHP Tutorials]]></category>
		<category><![CDATA[PHP5 OOPS Tutorials]]></category>
		<category><![CDATA[PHP5]]></category>
		<category><![CDATA[PHP5 OOPS]]></category>
		<category><![CDATA[PHP5 Tutorials]]></category>

		<guid isPermaLink="false">http://www.sunilb.com/php/php-tutorials/php-5-tutorials-static-data-members-and-methods</guid>
		<description><![CDATA[In this tutorial you will learn all about static data members and methods Meaning of static data members Meaning of static methods Defining static data members in PHP5 Defining static methods in PHP5 Accessing static data members in PHP5 Accessing static methods in PHP5 Rules to keep in mind for static methods Meaning of static [...]]]></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%2Fphp-5-tutorials-static-data-members-and-methods"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.sunilb.com%2Fphp%2Fphp-5-tutorials-static-data-members-and-methods&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>In this tutorial you will learn all about static data members and methods</p>
<ul>
<li>Meaning of static data members</li>
<li>Meaning of static methods</li>
<li>Defining static data members in PHP5</li>
<li>Defining static methods in PHP5</li>
<li>Accessing static data members in PHP5</li>
<li>Accessing static methods in PHP5</li>
<li>Rules to keep in mind for static methods</li>
</ul>
<p><span id="more-79"></span></p>
<h3>Meaning of static data members</h3>
<p>A data member that is commonly available to all objects of a class is called a static member. Unlike regular data members, static members share the memory space between all objects of the same class.</p>
<h3>Meaning of static methods</h3>
<p>A static method is a class method that can be called without creating an instance of a class. Such methods are useful when creating utility classes.</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>
<h3>Defining static data members in PHP5</h3>
<p>To define a static member in PHP5 you need to prefix the class member name with the keyword &#8216;static&#8217;. Look at the example below.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Customer <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$first_name</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// regular member</span>
	static <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$instance_count</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//static data member</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<blockquote><p>In the above example $instance_count is declared as a static data member</p></blockquote>
<h3>Defining static methods in PHP5</h3>
<p>To define a static data methods in PHP5 you need to prefix the class method name with the keyword &#8216;static&#8217;. Look at the example below.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&nbsp;
<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> getFirstName<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;">//body of method</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	static <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getInstanceCount<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;">//body of method</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<blockquote><p>In the above example getInstanceCount is declared as a static method</p></blockquote>
<h3>Accessing static data members in PHP5</h3>
<p>A static member data can be accessed using the name of the class along with the scope resolution operator (::) i.e. you donâ€™t need to create an instance of that class</p>
<p>Look at the example below:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Customer <span style="color: #009900;">&#123;</span>
&nbsp;
	static <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$instance_count</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//static data member</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		Customer<span style="color: #339933;">::</span><span style="color: #000088;">$instance_count</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> __destruct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		Customer<span style="color: #339933;">::</span><span style="color: #000088;">$instance_count</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> getFirstName<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;">//body of method</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	static <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getInstanceCount<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;">//body of method</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;">$c2</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>
&nbsp;
<span style="color: #b1b100;">echo</span> Customer<span style="color: #339933;">::</span><span style="color: #000088;">$instance_count</span><span style="color: #339933;">;</span></pre></div></div>

<p><strong>Output:</strong><br />
2</p>
<blockquote><p>In the above example, $instance_count is a static data member. Every time a new object is created the constructor is executed and the $instance_count variable is incremented by one. To echo the value contained in $instance_count variable, we use the :: (scope resolution) operator.</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>Accessing static method in PHP5</h3>
<p>A static method can be accessed using the name of the class along with the scope resolution operator (::) i.e. you donâ€™t need to create an instance of that class. However, you can also access it with an instance variable.</p>
<p>Look at the example below:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Customer <span style="color: #009900;">&#123;</span>
&nbsp;
	static <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$instance_count</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//static data member</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		Customer<span style="color: #339933;">::</span><span style="color: #000088;">$instance_count</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> __destruct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		Customer<span style="color: #339933;">::</span><span style="color: #000088;">$instance_count</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> getFirstName<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;">//body of method</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	static <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getInstanceCount<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">return</span> Customer<span style="color: #339933;">::</span><span style="color: #000088;">$instance_count</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;">$c2</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>
&nbsp;
<span style="color: #b1b100;">echo</span> Customer<span style="color: #339933;">::</span><span style="color: #004000;">getInstanceCount</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;">//this is using the scope resolution operator</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$c1</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getInstanceCount</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;">//this is using the instance variable</span></pre></div></div>

<p><strong>Output:</strong><br />
2<br />
2</p>
<h3>Rules to keep in mind for static methods</h3>
<ul>
<li>A static method can only access static data members</li>
<li>A static method does not have access to the $this variable</li>
</ul>
<p>Please feel free to write back for any clarification.</p>
<p><strong>Subscribe to my newsletter:</strong></p>
<p><!--subscribe2--></p>
</ol>
<p><strong>Related Posts on <b>PHP5 Tutorial</b> &#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/php5-tutorials-abstract-class-and-interface" title="PHP5 Tutorials - Abstract Class and Interface">PHP5 Tutorials &#8211; Abstract Class and Interface</a></li>
<li><a href="http://www.sunilb.com/php/php5-oops-tutorials-polymorphism" title="PHP5 OOPS Tutorials - Polymorphism">PHP5 OOPS Tutorials &#8211; Polymorphism</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/php-5-tutorials-static-data-members-and-methods/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>PHP5 OOPS Tutorials &#8211; Polymorphism</title>
		<link>http://www.sunilb.com/php/php5-oops-tutorials-polymorphism</link>
		<comments>http://www.sunilb.com/php/php5-oops-tutorials-polymorphism#comments</comments>
		<pubDate>Mon, 11 Feb 2008 07:09:09 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[PHP Class Examples]]></category>
		<category><![CDATA[PHP5 OOPS Tutorials]]></category>
		<category><![CDATA[PHP5]]></category>
		<category><![CDATA[PHP5 OOPS]]></category>
		<category><![CDATA[PHP5 Tutorials]]></category>

		<guid isPermaLink="false">http://www.sunilb.com/php/php5-oops-tutorials-polymorphism</guid>
		<description><![CDATA[In this tutorial we will learn the following: Meaning of Polymorphism Why method polymorphism cannot be achieved in PHP PHP 5 Object Polymorphism Meaning of Polymorphism Polymorphism is derived from two Greek words. Poly (meaning many) and morph (meaning forms). Polymorphism means many forms. In C you have two methods with the same name 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-tutorials-polymorphism"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.sunilb.com%2Fphp%2Fphp5-oops-tutorials-polymorphism&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>In this tutorial we will learn the following:</p>
<ul>
<li>Meaning of Polymorphism</li>
<li>Why method polymorphism cannot be achieved in PHP</li>
<li>PHP 5 Object Polymorphism</li>
</ul>
<p><span id="more-78"></span></p>
<h3>Meaning of Polymorphism</h3>
<p>Polymorphism is derived from two Greek words. Poly (meaning many) and morph (meaning forms). Polymorphism means many forms. In C you have two methods with the same name that have different function signatures and hence by passing the correct function signature you can invoke the correct method.</p>
<p>This is how polymorphism is achieved in languages like C where in a function sum(int, int) differs from sum(float, float). Therefore the method sum() has many forms depending on the parameters being passed to it.</p>
<p>The meaning with <b>Object Oriented</b> languages changes. With Object Oriented language <b>polymorphism</b> happens:</p>
<p>When the decision to invoke a function call is made by inspecting the object at runtime it is called Polymorphism</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>
<h3>Why method polymorphism cannot be achieved</h3>
<p>The reason why polymorphism for methods is not possible in PHP is because you can have a method that accepts two parameters and call it by passing three parameters. This is because PHP is not strict and contains methods like func_num_args() and func_get_arg() to find the number of arguments passed and get a particular parameter.</p>
<p>Because PHP is not type strict and allows variable arguments, this is why method polymorphism is not possible.</p>
<h3>PHP 5 Polymorphism</h3>
<p>Since PHP 5 introduces the concept of Type Hinting, polymorphism is possible with class methods. The basis of polymorphism is Inheritance and overridden methods.</p>
<p><strong>Lets look at an example:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #000000; font-weight: bold;">class</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> myMethod<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;BaseClass method called&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> 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> myMethod<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;DerivedClass method called&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;">function</span> processClass<span style="color: #009900;">&#40;</span>BaseClass <span style="color: #000088;">$c</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #000088;">$c</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">myMethod</span><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: #000088;">$c</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>
processClass<span style="color: #009900;">&#40;</span><span style="color: #000088;">$c</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<blockquote><p>
In the above example, object $c of class DerievedClass is executed and passed to the processClass() method. The parameter accepted in processClass() is that of BassClass. Within the processClass() the method myMethod() is being called. Since the method is being called on the class variable of BaseClass, it would not be wrong to assume that myMethod() of class BaseClass will be called. But, as per the definition &#8220;When the decision to invoke a function call is made by inspecting the object at runtime it is called Polymorphism&#8221;, myMethod() will be called on object DerievedClass. The reason why this happens is because the object of DerievedClass is being passed and hence the method myMethod() of DerievedClass will be called.
</p></blockquote>
<p>Please feel free to write back for any clarification.</p>
<p><strong>Subscribe to my newsletter:</strong></p>
<p><!--subscribe2--></p>
</ol>
<p><strong>Related Posts on <b>PHP5 Tutorial</b> &#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/php5-tutorials-abstract-class-and-interface" title="PHP5 Tutorials - Abstract Class and Interface">PHP5 Tutorials &#8211; Abstract Class and Interface</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-oops-tutorials-polymorphism/feed</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<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>6</slash:comments>
		</item>
		<item>
		<title>PHP Script to print nested array</title>
		<link>http://www.sunilb.com/php/php-script-to-print-nested-array</link>
		<comments>http://www.sunilb.com/php/php-script-to-print-nested-array#comments</comments>
		<pubDate>Fri, 16 Nov 2007 05:28:35 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[PHP Code Examples]]></category>

		<guid isPermaLink="false">http://www.sunilb.com/php/php-script-to-print-nested-array</guid>
		<description><![CDATA[Here is a script that will allow you to print a nested array upto any level. Can I request you to please review this and let me know how this can be further optimized. $array&#91;&#34;sunil&#34;&#93; = &#34;Bhatia&#34;; $array&#91;0&#93; = array&#40;&#34;one&#34;=&#62;&#34;two&#34;,array&#40;1,2,3,array&#40;4,5,6&#41;&#41;&#41;; $array&#91;&#34;sunil2&#34;&#93; = &#34;Bhatia2&#34;; &#160; processArray&#40;$array&#41;; &#160; $count=0; &#160; function processArray&#40;$array&#41; &#123; &#160; global $count; &#160; [...]]]></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%2Fphp-script-to-print-nested-array"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.sunilb.com%2Fphp%2Fphp-script-to-print-nested-array&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Here is a script that will allow you to print a nested array upto any level. Can I request you to please review this and let me know how this can be further optimized.</p>
<p><span id="more-75"></span></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$array</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;sunil&quot;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Bhatia&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$array</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;one&quot;</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;two&quot;</span><span style="color: #339933;">,</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">6</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$array</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;sunil2&quot;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Bhatia2&quot;</span><span style="color: #339933;">;</span>
&nbsp;
processArray<span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$count</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> processArray<span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$count</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000088;">$count</span><span style="color: #339933;">++;</span> <span style="color: #666666; font-style: italic;">//this variable is for calculating tab space</span>
&nbsp;
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$value</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: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				processArray<span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$key</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: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span> <span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$count</span> <span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&amp;nbsp;&amp;nbsp;&quot;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//change this to space ' ' if running from CLI</span>
				<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$value</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&lt;br /&gt;&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;">$count</span><span style="color: #339933;">--;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<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-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/php-script-to-print-nested-array/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>PHP5 Tutorial &#8211; Inheritance in PHP5</title>
		<link>http://www.sunilb.com/php/php5-tutorial-inheritance-in-php5</link>
		<comments>http://www.sunilb.com/php/php5-tutorial-inheritance-in-php5#comments</comments>
		<pubDate>Thu, 15 Nov 2007 05:21:50 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[PHP Tutorials]]></category>
		<category><![CDATA[PHP5 OOPS Tutorials]]></category>

		<guid isPermaLink="false">http://www.sunilb.com/php/php-tutorials/php5-tutorial-inheritance-in-php5</guid>
		<description><![CDATA[In the tutorial we will understand what is Inheritance in general and how to inherit classes in PHP5. Definition of Inheritance: Inheritance is the mechanism of deriving a new class from an existing class. It allows a sub-class / child class to share/inherit the attributes and behaviors of a base-class or parent class. These inherited [...]]]></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-inheritance-in-php5"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.sunilb.com%2Fphp%2Fphp5-tutorial-inheritance-in-php5&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>In the tutorial we will understand what is Inheritance in general and how to inherit classes in PHP5.</p>
<p><strong>Definition of Inheritance:</strong></p>
<p>Inheritance is the mechanism of deriving a new class from an existing class. It allows a sub-class / child class to share/inherit the attributes and behaviors of a base-class or parent class. These inherited attributes and behaviors are usually modified by means of extension.</p>
<p><span id="more-74"></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><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>PHP5 Inheritance</strong></p>
<p>To inherit in PHP5, you should use the keyword &#8216;extends&#8217; in the class definition. In PHP5 only single inheritance is allowed. 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> Person <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;">$address</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> 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: #000000; font-weight: bold;">class</span> Customer <span style="color: #000000; font-weight: bold;">extends</span> Person <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$customer_id</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$record_date</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> getCustomerId<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_id</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> getCustomerName<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;">getName</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;">// getName() is in Person</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<blockquote><p>In the above example, class Customer extends from the Person class. This means that Customer class is the child class and the Person base class. The child class Customer extends the method getName() and calls it in the getCustomerName() method of the Customer class.</p></blockquote>
<p><strong>Inheritance and Access Specifiers</strong><br />
You can refer to the Access specifiers tutorial to understand what it means. Let&#8217;s understand the use of Access Specifiers with context to Inheritance.</p>
<p><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">Access specifiers</a> specify the level of access that the outside world (other objects and functions) have on the data members / member functions of the class. Please note that all data members and member functions are public by default.</p>
<p>Lets look at how three access specifiers viz. private, public and protected behave in Inheritance:</p>
<p><strong>1. private</strong></p>
<p>A private access specifier is used to hide data and member functions. A method or data member declared as private can only be accessed by the class itself and neither the outside program nor the derived class can have access to it. Lets look at an 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;">public</span> <span style="color: #000088;">$age</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> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$age</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: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">age</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$age</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: #0000ff;">&quot;Sunil&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;28&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Name : &quot;</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: #666666; font-style: italic;">//causes an error</span></pre></div></div>

<blockquote><p>In the above example, the statement;</p>
<p>echo &#8220;Name : &#8221; . $c->name;</p>
<p>causes an error as we are trying to access $name that has been declared as a private member variable. We can however access the $age data member without any limitation as its public. We will learn more about public later in this tutorial.</p></blockquote>
<p>The reason why data members are declared private is to avoid the outside programs to either accidently modify the values without necessary validation. If you make a data member private you should provide accessor/mutator methods to access. <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">Read more about accessor/mutator methods here</a> (You will have to scroll a bit down and reach to &#8220;Notes on Accessor and Mutator methods&#8221;)</p>
<p>Note: When you define a method as private that method can only be called from within that class and not from outside that is the global level script.</p>
<p><strong>2. public</strong></p>
<p>A public access specifier allows the data members and methods to be access from anywhere in the script. Lets look at an 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;">public</span> <span style="color: #000088;">$age</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> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$age</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: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">age</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$age</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: #0000ff;">&quot;Sunil&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;28&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Age : &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$c</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">age</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//prints 28</span></pre></div></div>

<blockquote><p>In the above example, the statement;</p>
<p>echo &#8220;Age : &#8221; . $c->;age;</p>
<p>prints 28 on the screen as $age is a public variable and hence can be accessed from anywhere in the script. Please note that if you declare any data member or method without a access specifier it is considered as &#8216;public&#8217;.</p></blockquote>
<p><strong>3. protected</strong></p>
<p>A protected access specifier allows the derived class to access the data member or member functions of the base class, whereas disallows global access to other objects and functions.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Person <span style="color: #009900;">&#123;</span>
	protected <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;">class</span> Customer <span style="color: #000000; font-weight: bold;">extends</span> Person <span style="color: #009900;">&#123;</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: #666666; font-style: italic;">//this works as $name is protected in Person</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>
<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>
<span style="color: #000088;">$c1</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: #339933;">;</span> <span style="color: #666666; font-style: italic;">//this causes error as $name is protected and not public</span></pre></div></div>

<blockquote><p>In the above example, the statement;</p>
<p>$this->name = $name;</p>
<p>in the setName() function is referring to the $name data member of the Person class. This access is only possible because the $name variable has been declared as protected. Had this been private; the above statement would have raised an error. Further, in the statement towards the end;</p>
<p>$c1->name = &#8220;Sunil&#8221;;</p>
<p>raises an error as $name in the Person class has been declared as protected and not public.</p></blockquote>
<p><strong>PHP5 Inheritance &#8211; Method Overriding</strong><br />
Lets learn how to override methods in PHP5, but before we understand how to override methods in PHP5; lets define method overriding.</p>
<p><strong>Definition of Method Overriding:</strong><br />
Method overriding is when the function of base class is re-defined with the same name, function signature and access specifier (either public or protected) of the derived class.</p>
<p>The reason to override method is to provide additional functionality over and above what has been defined in the base class. Imagine that you have a class by the name of Bird from which you derive two child classes viz. Eagle and Swift. The Bird class has methods defined to eat, fly, etc, but each of the specialized classes viz Eagle and Swift will have its own style of flying and hence would need to override the flying functionality.</p>
<p><strong>Lets look at an example with Bird:</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> Bird <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> fly<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Fly method of Bird Class called&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> Eagle <span style="color: #000000; font-weight: bold;">extends</span> Bird <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> fly<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Fly method of the Eagle Class called&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> Swift <span style="color: #000000; font-weight: bold;">extends</span> Bird <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> fly<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Fly method of the Swift Class called&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$e</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Eagle<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$s</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Swift<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$e</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fly</span><span style="color: #009900;">&#40;</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: #000088;">$s</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fly</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><strong>Output:</strong><br />
Fly method of the Eagle Class called<br />
Fly method of the Swift Class called</p>
<blockquote><p>In the above example, we create two objects of class Eagle and Swift. Each of these classes have overridden the method fly() and have provided their own implementation of the fly() method that has been extended from the Bird class. The manner in which they have been extended the Bird class fly() method is not called as both these classes have provided a new functionality for the fly() method.</p></blockquote>
<p><strong>Another example of function overriding in Inheritance</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> Person <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">function</span> calculateAge<span style="color: #009900;">&#40;</span><span style="color: #000088;">$dob</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;calculateAge called of Person Class<span style="color: #000099; font-weight: bold;">\n</span>&quot;</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: #000000; font-weight: bold;">extends</span> Person <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">function</span> calculateAge<span style="color: #009900;">&#40;</span><span style="color: #000088;">$dob</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;calculateAge called of Customer Class<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;">$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;">$p1</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Person<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$c1</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">calculateAge</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;something&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$p1</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">calculateAge</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;something More&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><strong>Output:</strong><br />
calculateAge called of Customer Class<br />
calculateAge called of Person Class</p>
<p><strong>PHP5 Inheritance &#8211; Invoking parent methods</strong><br />
When you override a method of the base class, it&#8217;s functionality is completely hidden unless it has been explicitly invoked from the child class. To invoke a parent class method you should use the keyword parent followed by the scope resolution operator followed by the name of the method as mentioned below:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">	parent<span style="color: #339933;">::</span><span style="color: #004000;">function_name</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<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> Person <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> showData<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;This is Person's showData()<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> Customer <span style="color: #000000; font-weight: bold;">extends</span> Person<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> showData<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		parent<span style="color: #339933;">::</span><span style="color: #004000;">showData</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;This is Customer's showData()<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;">showData</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><strong>Output:</strong><br />
This is Person&#8217;s showData()<br />
This is Customer&#8217;s showData()</p>
<blockquote><p>In the above example, look at the way in which the showData() function in the Customer child class is invoking the the Person parent class&#8217;s showData() function. When the program executes the showData() method if the Customer class is called which inturn calls the showData() function of the parent class. After the parent class&#8217;s showData() function complets its execution the remaining code in showData() function of the Customer class is executed.</p></blockquote>
<p><strong>PHP5 Inheritance &#8211; Invoking parent Constructor and Destructor</strong><br />
We can get the parent <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 constructor</a> and <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 Destructor</a> to be invoked in the same way as invoking the parent method, 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> Person<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;This is Person's __construct()<span style="color: #000099; font-weight: bold;">\n</span>&quot;</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> __destruct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;This is Person's __destruct()<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> Customer <span style="color: #000000; font-weight: bold;">extends</span> Person<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		parent<span style="color: #339933;">::</span>__construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;This is Customer's __construct()<span style="color: #000099; font-weight: bold;">\n</span>&quot;</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> __destruct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		parent<span style="color: #339933;">::</span>__destruct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;This is Customer's __destruct()<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></pre></div></div>

<p><strong>Output:</strong><br />
This is Person&#8217;s __construct()<br />
This is Customer&#8217;s __construct()<br />
This is Person&#8217;s __destruct()<br />
This is Customer&#8217;s __destruct()</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-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-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-tutorial-inheritance-in-php5/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>PHP5 Tutorial &#8211; Defining Class Constants</title>
		<link>http://www.sunilb.com/php/php5-tutorial-defining-class-constants</link>
		<comments>http://www.sunilb.com/php/php5-tutorial-defining-class-constants#comments</comments>
		<pubDate>Thu, 15 Nov 2007 04:46:36 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[PHP Tutorials]]></category>
		<category><![CDATA[PHP5 OOPS Tutorials]]></category>

		<guid isPermaLink="false">http://www.sunilb.com/php/php-tutorials/php5-tutorial-defining-class-constants</guid>
		<description><![CDATA[In PHP4 the only constants that we would declare were global constants. In PHP5 it is possible to define a class level constant. These constants are specific to the class and hence don&#8217;t clutter the global level constant space. To declare a constant in a class, PHP5 provides developers with a new keyword i.e. const; [...]]]></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-defining-class-constants"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.sunilb.com%2Fphp%2Fphp5-tutorial-defining-class-constants&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>In PHP4 the only constants that we would declare were global constants. In PHP5 it is possible to define a class level constant. These constants are specific to the class and hence don&#8217;t clutter the global level constant space.</p>
<p><span id="more-73"></span><br />
<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>To declare a constant in a class, PHP5 provides developers with a new keyword i.e. const; 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;">const</span> TYPES <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Anything&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Types are : &quot;</span> <span style="color: #339933;">.</span> Customer<span style="color: #339933;">::</span><span style="color: #004000;">TYPES</span><span style="color: #339933;">;</span></pre></div></div>

<blockquote><p>In the above example, const is a keyword and TYPES is the name of the constant. Outside the class definition we echo the value of the constant by using the scope resolution operator (::) like this Customer::TYPES. Observe that we don&#8217;t need to create an object of the class to make use of the constant.</p></blockquote>
<p>The next logical question is if we can create an object of the Customer class and using the scope resolution operator access the constant. The answer is no; reason &#8211; because a constant belongs to the class definition scope and not to an object.<br />
<strong>Example of accessing Constants within a function</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>
	<span style="color: #000000; font-weight: bold;">const</span> TYPES <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Anything&quot;</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> showConstant<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Echo from showConstant() : &quot;</span> <span style="color: #339933;">.</span> Customer<span style="color: #339933;">::</span><span style="color: #004000;">TYPES</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;">showConstant</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><strong>Output:</strong><br />
Echo from showConstant() : Anything</p>
<p><strong>Some observations on Constants</strong></p>
<ol>
<li>Variables defined as constants cannot be changed.</li>
<li>Only a string or numeric value can be assigned to a constant.</li>
<li>Arrays, Objects &amp; Expressions cannot be assigned to a constant.</li>
<li>A class constant can only be accessed via the scope resolution operator (::) executed on the class name.</li>
<li>A class constant cannot have &lt;a&gt;access specifiers&lt;/a&gt; assigned to it (private, public &amp; protected)</li>
</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-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-tutorial-defining-class-constants/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>PHP5 Tutorial &#8211; instanceOf Operator Explained</title>
		<link>http://www.sunilb.com/php/php5-tutorial-instanceof-operator-explained</link>
		<comments>http://www.sunilb.com/php/php5-tutorial-instanceof-operator-explained#comments</comments>
		<pubDate>Thu, 15 Nov 2007 03:52:46 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[PHP Tutorials]]></category>
		<category><![CDATA[PHP5 OOPS Tutorials]]></category>

		<guid isPermaLink="false">http://www.sunilb.com/php/php-tutorials/php5-tutorial-instanceof-operator-explained</guid>
		<description><![CDATA[PHP5 introduces a new operator by the name of instanceOf. instanceOf is used to check if two objects passed as operands belong to the same class. This check can also happen when an object is compared with a class name. In PHP4 a similar functionality existed with a method is_a(), which has been replaced by [...]]]></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-instanceof-operator-explained"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.sunilb.com%2Fphp%2Fphp5-tutorial-instanceof-operator-explained&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>PHP5 introduces a new operator by the name of instanceOf. instanceOf is used to check if two objects passed as operands belong to the same class. This check can also happen when an object is compared with a class name.</p>
<p>In PHP4 a similar functionality existed with a method is_a(), which has been replaced by the instanceOf operator in PHP5.</p>
<p><span id="more-72"></span><br />
<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><br />
<strong>Lets look at an 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> Person <span style="color: #009900;">&#123;</span>
	<span style="color: #339933;">...</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$p1</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Person<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$p2</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Person<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$p1</span> instanceof <span style="color: #000088;">$p2</span><span style="color: #009900;">&#41;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;True&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">else</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;False&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<blockquote><p>In the above example, we are comparing to check if $p1 and $p2 belong to the same class i.e. Person. In this case $p1 and $p2 both belong to the same class Person and hence the output is True. Please note that line number 8 can also be written as if($p2 instanceof $p1) and will yield the same output.</p></blockquote>
<p>You can also use instanceOf operator to compare an object with the name of the class, 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> Person <span style="color: #009900;">&#123;</span>
	<span style="color: #339933;">...</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$p1</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Person<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$p1</span> instanceof Person<span style="color: #009900;">&#41;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;True&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">else</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;False&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<blockquote><p>In the above example, on line number 7 $p1 object is being compared to check if its a Person type of object. In this case $p1 is a Person type of object and hence the output is True.</p></blockquote>
<p><strong>Behaviour of instanceOf operator in inheritance</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: #000000; font-weight: bold;">extends</span> Person <span style="color: #009900;">&#123;</span>
	<span style="color: #339933;">...</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$p1</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Person<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;">=</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>
&nbsp;
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$c1</span> instanceof <span style="color: #000088;">$p1</span><span style="color: #009900;">&#41;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;True&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">else</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;False&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<blockquote><p>In the above example, on line number 8 $c1 child class object is being compared with $p1 which is a parent class object. This is possible because Customer class is a child of the Person Class, just that the Customer class is a specialized form of the Person class and therefore this becomes possible. However the reverse is not possible, we cannot compare if($p1 instanceof $c1) and will result in an error.</p></blockquote>
<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-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-tutorial-instanceof-operator-explained/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

