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

    Objects by their very nature are very difficult to manage as strings or numbers. This makes it difficult to store them in a database, pass along to a script, or use them generally for storing values, such as, for example, setting one as a cookie. To solve this problem, PHP has a function called serialize(). This function takes a variable and turns it into a more manageable version of itself. The syntax of this function is:

    $var =serialize(object);

    And to return the variable to its original state:

    $object =unserialize($var);>

    Both functions are a bit like encryption. If you want to hide a text you turn it into something unreadable, and then if you want to show the text you return it to its plain text state. This is more or less what serialization is about.

    When using these functions make sure to include the class definitions on the page that you call unserialize().

    Both functions look for two special functions when serializing objects:

    serialize() checks to see whether your class has a function with the name  __sleep. If  that function is present, it  will be run prior to any serialization. It is responsible for cleaning up the object and then returning an array that includes the names of all variables of that object that should be serialized.

    The purpose of the   __sleep function is to close any database connections that an object may have, commit pending data, or perform similar cleanup tasks. The function is useful for saving large objects.

    unserialize() checks for the presence of a function with the name  __wakeup. If present, this function can reconstruct any resources that object may have.

    The intended use of __wakeup is to re-establish any database connections that may have been lost during serialization and perform other reinitialization tasks.

    Conclusion

    While coding with functions and objects makes programming easy, it can also slow down code execution, particularly on large projects. So take care when deciding to use it.



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