PHP
  Home arrow PHP arrow Using Inheritance, Polymorphism and Serialization with PHP Classes
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PHP

Using Inheritance, Polymorphism and Serialization with PHP Classes
By: Jacques Noah
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 15
    2006-08-15


    Table of Contents:
  • Using Inheritance, Polymorphism and Serialization with PHP Classes
  • Polymorphism
  • Class Functions Without Instances
  • Serializing

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Using Inheritance, Polymorphism and Serialization with PHP Classes
    ( Page 1 of 4 )

    If you are working with classes in PHP, you will sooner or later encounter inheritance, polymorphism, and serialization. The ability to use these three will help speed up your code writing. This article covers how to use them, and more.

    Introduction

    Inheritance, in object oriented terms, allows us to create subclasses of a given class. Think of it as a hierarchy of different kinds of classification. For example, in our previous article we used a class called human. Humans come in different forms; you have Africans, Europeans, Asians, and so forth. They all have similar attributes, e.g. legs, arms, eyes, and so on, but are different in other ways. Now, to translate these similarities and differences into code, we would use inheritance. In addition to inheritance we are going to discuss both polymorphism and serialization, among other topics.

    Inheritance

    The syntax for creating an inherited class is:

    Class new_class_name extends original_class_name{}

    Let's look at our human class. The class as it stands now is very broad. Say we  want to specify an African person with this class. This is what we will do:

    class human{
    function human($hcolor){
         $this->hcolor=$hcolor;
    }
    var $legs=2;
    function report(){
    return  "This <b>".get_class($this)."</b> has <b>" .$this-
    >hcolor. "</b> hair,and <b>" .$this->legs. "</b> legs<br>" ;
    }
    }
    class African extends human{
    }
    //instantiate the class
    $jude = new human("black");
    $jane = new african("brown");
    $jude->legs++; //increase legs by one
    echo $jane->report();
    echo $jude->report();

    In the above code we added a new class (African) which inherited everything from the human class. And we created a instance of this new class called jane. This is what we get when we run this code:

    As you can see from the result, the new class name (african) is now shown. There is virtually no difference between the African class and the human class. This is because the extends keyword is causing the new class to inherit everything from the human class. So, the African is in effect, at the moment, the same as human. So how do we make differences between the two? We do this by specifying, within the new African class, certain variations:

    class human{
    function human($hcolor){
           $this->hcolor=$hcolor;
    }
    var $legs=2;
    function report(){
    return  "This <b>".get_class($this)."</b> has <b>" .$this-
    >hcolor. "</b> hair,and <b>" .$this->legs. "</b> legs<br>" ;
    }
    }
    class African extends human{
    var $hcolor=”black”;
    function African() {}
    }
    //instantiate the class
    $jude = new human("black");
    $jane = new african();
    $jude->legs++; //increase legs by one
    echo $jane->report();
    echo $jude->report();

    In the code above we added a variable called hcolor. We did this because the African class represents a new type of human. Now, we need to disable the constructor function human, because the African class inherits everything from the human class including its constructor function, so we need to create an equivalent function in the African class. So we add:

    function African() {}
    }

    This effectively overwrites the  constructor function of the human class. Note that this function does not take any arguments.

    As the results above show, the hair color for the African class is black. It will always be black no matter what we add to the jane object, because the hair color specified in the new African class is declared as black.



     
     
    >>> More PHP Articles          >>> More By Jacques Noah
     

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek