Home > PHP, PHP Tutorials, PHP5 Magic Methods, PHP5 OOPS Tutorials > PHP5 Tutorial – Magic Methods – __isset() and __unset()

PHP5 Tutorial – Magic Methods – __isset() and __unset()

November 3rd, 2007 admin Leave a comment Go to comments

In my previous PHP5 OOPS tutorial on PHP5 Tutorial – Magic Methods – __get() and __set(), we learnt how and when to use these magic methods.

This PHP5 OOPS tutorial will teach you how and when to use the magic methods __isset() and __unset().




These methods are automatically called internally when isset() and unset() is called on undeclared data members. The magic method __isset() method receives an argument – the value of which is the name of the variable that the program wants to test if the variable is set or not.

The magic method __unset() method receives an argument – the value of which is the name of the variable that the program wants to unset.

Look at the example below:

class Customer {
	private $data = array();
 
	public function __set($dt, $vl) {
		$this->data[$dt] = $vl;
	}
 
	public function __get($dt) {
		return $this->data[$dt];
	}
 
	public function __isset($dt) {
		return isset($this->data[$dt]);
	}
 
	public function __unset($dt) {
		return unset($this->data[$dt]);
	}
}
 
$c = new Customer();
$c->name = "Sunil Bhatia";
 
echo isset($c->name)."\n";
echo unset($c->name);




In the example above the script creates a new Customer Object. The program assigns a string value to an undeclared variable i.e. $c->name. The undeclared variable is handled by the magic method __set(). Read this post on PHP5 Magic Methods __set() if you need more understanding how the magic method __set() works.

The program ties to check if the undeclared variable i.e., $c->name has been set or not using the PHP method isset(). Since $c->name is an undeclared variable the PHP5 magic method __isset() is invoked that takes the name of the undeclared variable i.e. “name” and checks if the internal array $data["name"] is set or not.

Similarly, when the program calls unset() on the undeclared variable i.e. $c->name, the PHP5 magic method __unset() is invoked that takes the name of the undeclared variable i.e. “name” and unsets the internal array $data["name"].

Related Posts on PHP5 Tutorial – Object Oriented Programming (OOPS)

  1. PHP5 Tutorial – Learn to create a PHP5 Class
  2. PHP5 Tutorial – Learn to Create a PHP5 Class Object
  3. PHP5 Tutorial – Defining Attributes of a PHP5 Class
  4. PHP5 Tutorial – Defining Methods of a PHP5 Class
  5. PHP5 Tutorial – Creating a PHP5 Constructor __construct()
  6. PHP5 Tutorial OOPS – Creating a PHP5 Destructor __destruct()
  7. PHP5 Tutorial OOPS – PHP5 Class Access Specifiers – public, private and protected
  8. PHP5 Tutorial – Magic Methods – __toString() method
  9. PHP5 Tutorial – Magic Methods – __get() and __set()
  10. PHP5 Tutorial – Magic Methods – __isset() and __unset()
  11. PHP5 Tutorial – Magic Methods – __call() method
  12. PHP5 Tutorial – Magic Methods – __autoload() method
  13. PHP5 Tutorial – Magic Methods – __sleep() and __wakeup()
  14. PHP5 Tutorial – Magic Methods – __clone() method

Warning: session_start(): Cannot send session cookie - headers already sent by (output started at /home/sunilb/www.sunilb.com/wp-content/plugins/all-in-one-seo-pack/aioseop.class.php:245) in /home/sunilb/www.sunilb.com/wp-content/plugins/mycaptcha/MyCaptcha.php on line 41

Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /home/sunilb/www.sunilb.com/wp-content/plugins/all-in-one-seo-pack/aioseop.class.php:245) in /home/sunilb/www.sunilb.com/wp-content/plugins/mycaptcha/MyCaptcha.php on line 41
  1. faith
    November 25th, 2007 at 05:07 | #1

    public function __unset($dt) {
    return unset($this->data[dt]);
    }

    There’s a typo

    dt on line 2 -> $dt

  2. Muthu
    March 3rd, 2010 at 12:41 | #2

    unset won’t return anything, so it should be

    public function __unset($dt) {
    unset($this->data[$dt]);
    }

    And you can check whether the value got unset using :

    unset($c->name);
    echo ‘name=>’.$c->name;

    Thanks…

  3. BHARAT
    January 28th, 2012 at 09:36 | #3

    Error in Line no 17 ,22, 25 ,Kindly edit and replace your example.

  4. BHARAT
    January 28th, 2012 at 09:39 | #4

    Dear Sunil,

    Following error are display when we use your above example. Kindly edit it and replace and inform me.

    line no 17 : return unset($this->data[dt]);
    line no 22 : $c->name = “Sunil Bhatia”;
    line no 25 : echo unset($c->name);

  5. admin
    January 30th, 2012 at 05:02 | #5

    Thanks for pointing this out Bharat. I have fixed the issue – had some issue with UTF-8 encoding while saving the post.

  6. bharat
    January 31st, 2012 at 10:40 | #6

    Dear Sunil,

    Thanks for your great thought. We are waiting. We also requesting to you that kindly create some shopping card tutorial for e-commerce website purpose.

  1. November 3rd, 2007 at 15:04 | #1
  2. November 8th, 2007 at 22:43 | #2
Enter this code to leave comment (Sorry, but bots get me crazy :) )