PHP5 Tutorial – unset() Magic Method  

Check out latest Mobile Phones and Books only at Flipkart.com
Flipkart.com

This tutorial will guide you through the unset() Magic Method. The unset() magic method of PHP5 is called whenever unset function of PHP is called to clear an undeclared data member. With the help of this method we can check for the undeclared variables in the code. We can also set appropriate error message while testing for variable names getting used in the Class.

Syntax:

   function __unset($dt)
   {
      //$dt is the Variable Name that unset is supposed to check.
   }

Example – Basic __unset() Magic Method Usage

< ?
class magicmethod
{
	function __unset($variablename)
	{
		echo "Variable '".$variablename."' not Set and Cannot be UnSet";
	}
}
$a = new magicmethod();
unset($a->name);
?>

Output: Variable ‘name’ not Set and Cannot be UnSet
Explanation for the Example:

Here i am trying to destroy the name attribute using isset function of PHP.

But the name attribute is not defined inside magicmethod class so the PHP compiler call the __unset() method where an appropriate error message is displayed.


Related Articles:

Categories: PHP Tags: , ,
  1. No comments yet.
  1. No trackbacks yet.

 

Page optimized by WP Minify WordPress Plugin