PHP
  Home arrow PHP arrow Page 4 - Using the Clone Magic Function in PHP 5
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 the Clone Magic Function in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 3
    2009-06-15


    Table of Contents:
  • Using the Clone Magic Function in PHP 5
  • Review: the sleep and wakeup magic functions
  • Triggering functions in the background when cloning objects
  • Calling the clone method when cloning an object

  • 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 the Clone Magic Function in PHP 5 - Calling the clone method when cloning an object
    ( Page 4 of 4 )

    The best way to understand how the “__clone()” method is called by the PHP interpreter is by means of a concrete example that shows how to clone an instance of the sample “User” class.

    I developed a small script which uses the “clone” keyword to clone an user object, in this way triggering a call to the pertinent “__clone()” method. The definition of this example script is as follows:

    $user = new User();

    $user->fname = 'Alejandro';

    $user->lname = 'Gervasio';

    $user->email = 'alejandro@mydomain.com';

    // display user data

    echo 'First Name : ' . $user->fetch('fname') . ' Last Name : ' . $user->fetch('lname') . ' Email : ' . $user->fetch('email');

    /*

    displays the following

    First Name : Alejandro Last Name : Gervasio Email : alejandro@mydomain.com

    */

     

    // clone user object

    $newuser = clone $user;

    /*

    displays the following

    Cloning user object.

    */

    Simple to code and read, right? As you can see, once the script spawns an object from the “User” class and creates some undeclared properties, it clones this object. This process automatically calls the pertinent “__clone()” method.

    As with other magic functions reviewed before, this specific method supports a more complex and useful implementation. So, if the magic functions bundled with PHP 5 have already caught your attention, you might want to sharpen your programming skills by playing with this method a bit when cloning objects. The experience will be really instructive, trust me.

    Final thoughts 

    Over this fifth chapter of the series, I discussed how to implement and use the “__clone()” magic method, which as you saw earlier, is invoked automatically when an object is cloned via the “clone” keyword.

    In the penultimate installment of the series, I’m going to explore in depth another helpful magic method provided by PHP 5, probably one that you’ve used many times before when creating your classes.

    In this specific case, I’m talking about the handy “__destruct()” method, which is called by the PHP interpreter before an object gets destroyed at the end of a script.

    Want to learn how to use this method? Then don’t miss the next tutorial!



     
     
    >>> More PHP Articles          >>> More By Alejandro Gervasio
     

       

    PHP ARTICLES

    - Adding Ordering and Grouping Clauses to the ...
    - 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...





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