PHP
  Home arrow PHP arrow Page 3 - 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 - Class Functions Without Instances
    ( Page 3 of 4 )

    There is a way to use a class function without having to create an instance of a class. This capability has been added since PHP 4 and is useful if you only need a function from a class, but not the entire class itself. The syntax for doing this is:

    the_Classname::the_Method();

    To demonstrate, lets use the "say_hello()" function in our human class:

    <?
    class human{
    function human($hcolor){
           $this->hcolor=$hcolor;
    }
    function say_hello(){
    echo "Hello!<br>";
    }
    }
    echo "The result of the uninstantiated class function is: <br>";
    echo herehuman::say_hello();
    ?>

    As you can see from the code above, all I did was add the human::say_hello() line of code. The say_hello() function is a function of the human class. Normally, to use this function we would have to create an instance of the class, but in this case we did not, so let’s run the code and see what happens:

    The above screen shot shows the results of a class function used without instantiating the class.

    Remember to include the class from which you want to use the method. Also, as a matter of good coding practice, you can make sure the specific function exists in a class before using it. You do this like so:

    if(method_exist(the_classname::function_name())){
    //do something here…
    }

    To check that the say_hello() function exists in our human class:

    if(method_exist(human::say_hello())){
    //do something here..
    }

    Destroying Objects

    In most object oriented languages there is a built-in capability to free or destroy objects that you no longer want to use. Why do you need to destroy an object? Well, the reason is quite simple. The creation and use of an object is resource intensive, and it is therefore important to free up the resources that get tied up when using objects. This is done by calling the unset() function after you’ve instantiated the object:

    $object = new Classname
    unset($object);

    Remember to call this function at the very end of a script to avoid destroying the object while it is needed by the program.



     
     
    >>> 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 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek