Home arrow PHP arrow Page 2 - The Destruct Magic Function in PHP 5

Review: the clone magic function - PHP

Welcome to the sixth part of a seven-part series that shows you how to use the magic functions that come with PHP 5. In this article, I cover the “__destruct()” method, also known as a destructor. They can be really useful for performing all sorts of clean-up tasks, or for creating objects that are capable of maintaining their state across several HTTP requests.

TABLE OF CONTENTS:
  1. The Destruct Magic Function in PHP 5
  2. Review: the clone magic function
  3. Working with destructors in PHP 5
  4. Creating a persistable object with the destruct method
By: Alejandro Gervasio
Rating: starstarstarstarstar / 1
June 23, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
  

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.



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