PHP5 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
Popular Articles:
- OOPS in PHP5 – Define Methods for the Class
- How to Implement Text to Speech in PHP
- Writing HTML Scrapper in PHP
- PHP5 Tutorial – __wakeup() Magic Method
- HTTP FORM POST in PHP using AJAX
- PHP5 Tutorial – Not Pure Object Oriented Programming Approach
- OOPS in PHP 5 Tutorial – Parent Keyword
- MySql Prepared Statement in PHP
- OOPS in PHP 5 Tutorial – Abstract Class
- OOPS in PHP 5 – __destruct() Method


































