In case you still haven’t had the opportunity to read the last installment of the series, where I explained how to trigger the “__clone()” method when cloning an instance of the sample “User” class coded previously, below I included the source class of this class, along with a rudimentary example that shows how to use it. Here’s the class that implements the “__clone()” method:
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.'; } }
From the above code sample, it’s clear to see how easy it is to give a concrete implementation to the “__clone()” method. In this particular case, the method has been coded to display a message on screen when a user object is being cloned, but it’s perfectly possible to define it for performing more useful tasks. Now that you hopefully recalled how to include this magic method within a specific class, below there’s an example that demonstrates how things look behind the scenes when an instance of the “User” class is cloned via the “clone” keyword. Take a look at the following script:
$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. */
See how simple it is to put the “__clone()” magic method into action? I guess you do. In this case, when the “clone” keyword is utilized for cloning a user object, the method displays an indicative message on the browser. It’s that easy, actually. So far, so good. At this stage, you've surely learned how to implement the “__clone()” magic function within a class. So, it’s time to explore another handy method included with PHP 5. Yes, as you might have guessed, I’m talking about the “__destruct()” method, which unlike its counterpart “__construct()” is triggered before destroying a specified object. As I explained earlier, destructors can be used for performing all sorts of clever tasks, and taking this capability into account, in the next section I’m going to reuse the previous “User” class by adding a destructor to it, so you can learn the full details of this process. That being said, please click on the link that appears below and read the following segment. I’ll be there, waiting for you.
blog comments powered by Disqus |
|
|
|
|
|
|
|