PHP
  Home arrow PHP arrow Page 6 - Object Oriented Programming in PHP
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
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

Object Oriented Programming in PHP
By: Luis Argerich
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 183
    2003-01-13

    Table of Contents:
  • Object Oriented Programming in PHP
  • Data Members and Functions
  • Constructors
  • Overloading
  • OOP Programming in PHP
  • Copying and Cloning
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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


    Object Oriented Programming in PHP - Copying and Cloning


    (Page 6 of 7 )

    When you create an object $obj you can copy the object by doing $obj2=$obj, the new object is a copy (not a reference) of $obj so it has the state $obj had in the moment the assignment was made.

    Sometimes you don't want this you just want to create a new object of the same class as obj, calling the constructor of the new object as if you had used the new statement.

    This can be done in PHP using serialization and a base class that all other classes must extend.

    Entering a Danger Zone

    When you serialize an object you get a string which has a certain format, you may investigate this if you are curious. One of the things the string has is the name of the class (nice!), you can extract it using:

    <?php

    $herring=serialize($obj);
    $vec=explode(':',$herring);
    $nam=str_replace("\"",'',$vec[2]);

    ?>


    So suppose you create a class "Universe" and force that all classes must extend universe, you can define a method clone in Universe as:

    <?php

    class Universe {
    function clone() {
    $herring=serialize($this);
    $vec=explode(':',$herring);
    $nam=str_replace("\"",'',$vec[2]);
    $ret=new $nam;
    return $ret;
    }
    }


    Then:

    $obj=new Something();
    //Something extends Universe !!
    $other=$obj->clone();

    ?>


    What you get is a new object of class Something created the same way as using new; the constructor is called, etc. I don't know if this is useful for you but the Universe class which knows the name of the derived class is a nice concept to experiment with. The only limit is your imagination.

    Note: I'm using PHP4, some of these examples may not work in PHP3.

    More PHP Articles
    More By Luis Argerich


       · Thanks for the article.Just wanted to correct a typo..to make the internet a...
       · This article is pretty old, it needs updation for PHP version 5 and later.
       · This OOPs is very helpful me, but i expects with author to make it update plz for...
       · :rofl:PHP is not an OOP language in the first place. It's meant to be simple and...
       · Obviously the good people from PHP.net thought it was the right way to go seeing as...
     

    PHP ARTICLES

    - Making Usage Statistics in PHP
    - Installing PHP under Windows: Further Config...
    - File Version Management in PHP
    - Statistical View of Data in a Clustered Bar ...
    - Creating a Multi-File Upload Script in PHP
    - Executing Microsoft SQL Server Stored Proced...
    - Code 10x More Efficiently Using Data Access ...
    - A Few Tips for Speeding Up PHP Code
    - The Modular Web Page
    - Quick E-Commerce with PHP and PayPal
    - Regression Testing With JMeter
    - Building an Iterator with PHP
    - PHP Frontend to ImageMagick
    - Using PEAR's mimeDecode Module
    - Incoming Mail and PHP







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