Custom Search

PHP 5 Tutorial - __unset() Magic Method

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.

Custom Search

Related Post

Did you enjoy this post? Why not leave a comment below and continue the conversation, or subscribe to my feed and get articles like this delivered automatically to your feed reader.

Comments

No comments yet.

Leave a comment

(required)

(required)