Home arrow PHP Tutorials

Translate This Website
Sunday, 07 September 2008
Web Development
Request a Quote
Script Categories
RSS Media Grabber Funny Videos & Pictures Life Stories (Flash) Video Search & Download Video Downloader Script Country on Sale Advanced Polls Phone Upload/SMS Scripts PhotoCube Script SlideShow Creation Script Make Confessions Scripts Guitar Tabs Scripts Create Custom Smiley Script Short URLs & Subdomains Hot Or Not Game Scripts Put your text on images Watermark & Image Hosting Joomla Components Low Cost Scripts
Partners
WEBSITE TRAFFIC
Pissed Customers
Credit Cards
Broadband Tweaks & Info
eBay Sniping Software
Custom Programming
Scripts and Stuffs
Hot php scripts
Add your scripts
Script Search
php scripts directory
script heaven
the cgi site
Scripts
Software Development
Free Icons
SEO Services


Expanding PHP Classes Print E-mail

While you can always modify a class to add more properties and methods, there is a better way to create a more complex class based on a simple one. Extending existing classes by creating new classes which inherit their parent class properties and methods may come in handy when you need to create more classes based on the initial one. This is also called a "parent-child" relationship, the reasons are obvious. Know that the parent class must be defined before the child class, so the order in which the classes are defined is important. Let's get back to our automobile class, and create a new class based on the original one:

class used_automobile_class extends automobile_class

{

var $owner;

var $is_price_negotiable;

 

function write_negotiable()

{

if ($this->is_price_negotiable)

{

print "The price is negotiable!";

}

else

{

print "The price is NOT negotiable!";

}

}

}

 

We have created a new class that inherits the all of the properties and methods of "automobile_class", and adds some new ones. While you can always add new properties and methods, it is not possible to remove any of the properties or methods defined in the parent class. You can override them, though. This means that a child class can redefine a method of its parent class, and any new objects created based on the child class will use the child’s redefined method.

After reading this, you may wonder why you should expand existing classes, and not just rewrite the class and adapt it to the new requirements. The answer lies in flexibility. There are times when you start writing a class from scratch, add some more properties as needed and create this large class, but then you want to get back to one of the initial structures and redefine it some other way. This would not have happened if you had created children classes that expanded the original class.

If you have worked with OOP before on other programming languages, you may be wondering “how about the constructors and destructors?” If you don’t know what I’m talking about, you should be interested to know that PHP allows you to automatically call a special method (also known as a constructor) when you create a new instance of the class using “new”. Destructors, on the other hand, are special methods that are automatically called when an object is destroyed. Unfortunately, there are no destructors in PHP, but you may use “register_shutdown_function()” instead to simulate most effects of a destructor.

Creating a constructor is easy, you only have to create a method with the same name as the class. In the following example, we output a string and set a variable when an object is created based on this class:

class automobile_class

{

var $negotiable_price;

var $price; //the price of the car, in dollars

 

function automobile_class()

{

print “Object created!”;

this->$negotiable_price = FALSE;

}

}

 

Please take notice of the fact that in PHP there are some class and methods names that are reserved to language. A small constraint is that you cannot name a class "stdClass", because this is internally used by PHP. Also, it is recommended that you don’t use function names beginning with "__", they are “magical” to PHP.




Credit: www.softwareprojects.org




Reddit!Del.icio.us!Facebook!Slashdot!Netscape!Technorati!StumbleUpon!Newsvine!Furl!Yahoo!Ma.gnolia!Free social bookmarking plugins and extensions for Joomla! websites!
 
< Prev   Next >
Services
Script Installation
Hosting
Free Image Hosting
PageRank Checker
Login Form
On Sale
RSS Media Grabber - Get 100s of Videos
RSS Media Grabber - Get 100s of Videos
$129.00
$79.00
You Save: $50.00
Add to Cart
Home Announcements Support Newsletter Forum PHP Articles PHP Tutorials Scripts FAQs Free Scripts Links Contacts
Copyrighted © 2008 phppod.com