Home arrow PHP arrow Page 4 - Using the Clone Magic Function in PHP 5

Calling the clone method when cloning an object - PHP

If you’re an eager PHP developer who wants to have at your disposal a quick guide that shows you how to work with the most relevant magic functions provided by PHP 5, then this series of articles might be what you’re looking for. In this fifth part of a seven-part tutorial on magic functions, we'll briefly review the sleep and wakeup functions, and then tackle the clone function.

  1. Using the Clone Magic Function in PHP 5
  2. Review: the sleep and wakeup magic functions
  3. Triggering functions in the background when cloning objects
  4. Calling the clone method when cloning an object
By: Alejandro Gervasio
Rating: starstarstarstarstar / 4
June 15, 2009

print this article



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: