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

Triggering functions in the background when cloning objects - 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.

TABLE OF CONTENTS:
  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
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

As I explained at the beginning of this tutorial, PHP 5 also includes a magic method called ”__clone(),” which is invoked automatically when using the “clone” keyword. For the sake of brevity, for now I’m going to say only that the “clone” keyword is used to create an independent copy of a specified object, instead of creating a reference to it.

Now, returning to the “__clone()” magic method, it’s possible to give it a concrete implementation, in a way similar to the process performed with other magic functions discussed previously. To accomplish this, I’m going to reuse the “User” class that you saw in the previous section, which now will have the following definition:

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 __clone( method

public function __clone()

{

echo 'Cloning user object.';

}

}

In this particular case, I decided to give a trivial implementation to the above “__clone()” method to make it simpler for you to grasp, but naturally it can be set up to perform more complex tasks.

However, despite how simple the “__clone()” method may look at first glance, it will be useful to demonstrate how it can be called by the PHP engine automatically when cloning an instance of the sample “User” class.

If you wish to learn the full details of this process, though, you’ll have to read the last section of this tutorial.



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

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