PHP
  Home arrow PHP arrow Page 2 - 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? 
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 - Polymorphism
    ( Page 2 of 4 )

    The idea of polymorphism is that when we look at an object, and ask it to do something, like call one of its methods, we do not really need to know how a class works or exactly how it does its internal workings. We need to know how it works and what it does when we are defining the class and writing the methods, but we do not need to know, as programmers making use of the object, exactly how it all happens.

    For instance, we can have two different objects with methods that have the same name, which will do similar things -- but with a difference depending on the kind of object we call the method from. The principal of polymorphism states that we do not need to specify anything different when we ask the object to do whatever it is that we want it to do. To demonstrate, let's  make some changes to our human class:

    class human{
    function human($hcolor){
           $this->hcolor=$hcolor;
    }
    function say_hello(){
    echo "Hello!<br>";
    }
    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->say_hello();
    echo $jude->say_hello();

    If we want the African class to say something other than hello, here’s what we do:

    class human{
    function human($hcolor){
           $this->hcolor=$hcolor;
    }
    function say_hello(){
    echo "Hello!<br>";
    }
    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() {}
    function say_hello(){
    echo “Hello there!”
    }
    }
    //instantiate the class
    $jude = new human("black");
    $jane = new african();
    $jude->legs++; //increase legs by one
    echo $jane->say_hello();
    echo $jude->say_hello();

    We asked both objects to say hello using exactly the same syntax and it worked as expected. This is important in object oriented programming because it allows the object to take control of the way it works. In other words, we can make the same request of two different objects, and based on the different workings of the two objects, we end up with two (possibly different) results. This makes coding cleaner and more intuitive.



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

       

    PHP ARTICLES

    - 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
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    Stay green...Green IT