Home arrow PHP arrow Page 4 - The Isset and Unset Magic Functions in PHP 5

Introducing the isset and unset magic functions - PHP

Welcome to the second part of a seven-part series on the magic functions in PHP 5. In the previous article, we looked at property overloading with the get and set functions. In this one, we'll take a look at the same task using the isset and unset magic functions.

  1. The Isset and Unset Magic Functions in PHP 5
  2. Review: the set and get magic functions
  3. Determining what properties to create within a class
  4. Introducing the isset and unset magic functions
By: Alejandro Gervasio
Rating: starstarstarstarstar / 3
June 02, 2009

print this article



As their names clearly suggest, the “__isset()” and “__unset()” magic functions are automatically called by the PHP engine when the popular “isset()” and “unset()” functions are used in that sequence within a script.

As with all of the other magic functions, these have no concrete implementation, which allows us to create “hooks” very easily when unsetting and checking the existence of a certain PHP variable.

But surely you've learned all of this theory over and over again each time you looked at the PHP manual, so let’s proceed to code a simple example that shows the “__isset()” and “__unset()” functions in action.

Remember the “User” class that I utilized in the previous segment? Good. Based on its definition, I’m going to give an explicit implementation to the aforementioned functions, to show you how they can be used in a concrete case.

Below is the class with the appended “__isset()” and “__unset()” functions:

class User


// constructor (not implemented)

public function _construct(){}


// set undeclared property in a restrictive way

public function __set($property, $value)


if (in_array($property, array('fname', 'lname', 'email')) === TRUE)


$this->$property = $value;




// get declared property

public function __get($property)


return $this->$property;



// implement __isset() method

public function __isset($property)


echo 'Checking if property '. $property . ' has been set...';



// implement __unset() method

public function __unset($property)


echo 'Unsetting property ' . $property;



As shown above, the implementation of the “__isset()” and “__unset()” methods is admittedly very trivial but hopefully illustrative. In the first case, if the “isset()” function is called with an instance of the “User” class, then a brief message will be displayed on screen, informing of the occurrence of this process.

On the other hand, if “unset()” is used with a user object, then the complementary “__unset()” method will be called automatically as well.

Want to see how this description can be translated into functional code? Well, take a look at the following code sample to dissipate all of your doubts:

$user = new User();

$user->fname = 'Alejandro';

$user->lname = 'Gervasio';



display the following

Checking if property email has been set...





displays the following

Unsetting property email


Hopefully, the above example demonstrates in a clear fashion the behavior of the “__isset()” and “__unset()” methods. Of course, this is only an example, which means that it’s feasible to give more complex implementations to the methods in question to make them perform more useful tasks. But, that will be left as homework for you, so you can build a killer script that makes these functions really shine!

Final thoughts

That’s about it for now. In this second tutorial of the series I demonstrated how to extend the use of the “__set()” and “__get()” magic functions to build a simple mechanism that controls which properties of a class must be created. I also explained a trivial use of the “__isset()” and “__unset()” functions, which as you saw previously, are actually pretty simple to understand.

In the upcoming article, I’m going to discuss how to overload methods of a class via the “__call()” magic function. Now that you’ve been told the topic that will be covered in that tutorial, you can miss it!

>>> More PHP Articles          >>> More By Alejandro Gervasio

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: