__isset & __unset in PHP 5

-

Introduction

The magic methods __isset and __unset are automatically called when the built in functions isset and unset are used.

When using the _isset an argument must be parsed to the function containing the property that you want to test against. When I say test, you are checking to see if that value has been set or not.

When using the __unset an argument must also be parsed to the function. This argument contains the name of the property that you want to unset

 

Example __isset and __unset

<?php
  class WebDeveloper {
    public $info = array();
    public function __get($item) {
      $this->info[$item] = $value;
    }
    public function __set($item, $value) {
      $this->info[$item] = $value;
    }
    public function __isset($item) {
      return isset($this->info[$item]);
    }
    public function __unset($item) {
      unset($this->info[$item]);
    }
  }
  $developer = new WebDeveloper();
  $developer->__set(‘name’, ‘Jordizle’);
  if(isset($developer->name)){ 
    // do something 
  }
  unset($developer->name);
?>

The example above creates a new instance of the ‘WebDeveloper’ object and sets the first and second name. The code then uses the ‘isset’ function which then automatically calls the __isset function within the object which checks and returns for the parsed value in the ‘info’ array.

I have used an ‘if’ statement which could be used to perform a task depending if a values exist. Once the ‘if’ statement is finished, I have unset the value from the object.  

Your comments.