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

Review: the sleep and wakeup magic functions - 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

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
 

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: