PHP5 Tutorial – unset() Magic Method
Check out latest Mobile Phones and Books only at 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:
- PHP5 Tutorial – Not Pure Object Oriented Programming Approach
- OOPS in PHP 5 Tutorial – Abstract Class
- OOPS in PHP 5 – destruct() Method
- PHP 5 Tutorial – get() Magic Method
- OOPS in PHP 5 – Polymorphism
- OOPS in PHP 5 Tutorial – Function Overriding
- PHP5 Tutorial – Parsing XML documents in PHP5 using SimpleXML
- PHP5 Tutorial – isset() Magic Method
- OOPS in PHP 5 – instanceOf Operator
- PHP5 Tutorial – wakeup() Magic Method
Recent Comments