PHP
  Home arrow PHP arrow Page 2 - 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 - Review: the sleep and wakeup magic functions
    ( Page 2 of 4 )

    Before I get my hands dirty explaining how to work with the “__clone()” magic function when cloning objects, I’d like to recreate the example shown in the previous article. It demonstrated how to use the  “__sleep()” and “__wakeup()” methods within a basic class called “User.”

    Having said that, please look at the full source code of the class in question, which originally was defined like this:

    class User

    {

    // constructor (not implemented)

    public function _construct(){}

     

    // set undeclared property in a restrictive way

    public function __set($property, $value)

    {

    if (in_array($property, array('fname', 'lname', 'email')) === TRUE)

    {

    $this->$property = $value;

    }

    }

     

    // get undeclared property

    public function __get($property)

    {

    if (isset($this->$property))

    {

    return $this->$property;

    }

    }

     

    // single point to fetch user data

    public function __call($method, $args)

    {

    if ($method === 'fetch' AND empty($args) === FALSE)

    {

    return $this->$args[0];

    }

    }

     

    // implement __sleep() method

    public function __sleep()

    {

    echo 'Serializing user object';

    return array('fname');

    }

     

    // implement __wakeup() method

    public function __wakeup()

    {

    echo 'Unserializing user object';

    }

    }

    As depicted above, the “User” class gives a simple implementation to both the “__sleep()” and “__wakeup()” magic methods, which in this particular case are only responsible for displaying a rather silly message on the browser.

    Nevertheless, the most interesting things happen when an instance of the class that you saw before is serialized and unserialized alternately. This process is clearly illustrated by the example below:

    // example on using the 'User' class with the '__sleep()' and '__wakeup()' methods

     

    $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

    */

     

    // serialize user object

    $user = serialize($user);

    /*

    displays the following

    Serializing user object

    */

     

    // unserialize user object

    var_dump(unserialize($user));

    /*

    displays the following

    object(User)[1]

    public 'fname' => string 'Alejandro' (length=9)

    */

    As you can see, not only is it possible to use a non-existent “fetch()” method to retrieve the values assigned to the properties of a user object, but this process is complemented with the proper calls to the “__sleep()” and “__wakeup()” functions when the object in question is serialized and unserialized alternately.

    Assuming that you’ve grasped the logic that drives the example above, it’s time to explore other magic functions bundled with PHP 5. Therefore, in accordance with the concepts deployed in the introduction of this article, in the next section I’m going to discuss how to use the “__clone()” method. As you might have guessed, it is called by the PHP interpreter when cloning an object via the corresponding “clone” keyword.

    To learn more about this interesting topic, click on the link below and read the next few lines.



     
     
    >>> 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