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.
blog comments powered by Disqus |
|
|
|
|
|
|
|