The Isset and Unset Magic Functions in PHP 5

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.

As you may have heard, PHP 5 comes packaged with a set of unimplemented functions that are popularly known as “magic functions” or “magic methods.” These are called internally by the PHP engine when performing a number of tasks, such as including classes that haven’t been found at runtime, when attempting to assign values to undeclared properties of a class, or when a script tries to call a non-existent method, only to cite a few examples.

Also, magic functions will be invoked transparently by the PHP parser when serializing and unserializing objects, as well as when creating and destroying instances of a class. This allows us to execute additional processes and trigger predefined actions behind scenes in an easy way.

Obviously, giving a concrete implementation to this arsenal of functions permits us to do to all sorts of clever things with a minimal amount of code. Please remember that this may eventually be detrimental to the readability and maintenance of an application.

Despite these issues, there are times when the implementation of particular magic functions, like “__set()” and “__get()” for instance, comes in handy for easily overloading properties of a class. And now that I’ve brought the subject to the table, in the first chapter of this series, I used a variety of code samples to demonstrate how to overload properties of a sample class called “User” by using the complementary “__set()” and “__get()” magic functions.

Those examples showed how to dynamically create undeclared properties of the aforementioned class; they also demonstrated how to retrieve their values, either for display purposes or for further processing.

However, overloading properties is only one of the many useful things that can be accomplished with magic functions. It’s also possible to perform additional tasks when working with the “isset()” and “unset()” PHP functions, but in this case by using another set of complementary functions, called “__isset()" and “__unset()” respectively.

So, in the next few lines I’m going to delve more deeply into the advantages of using property overloading, and then take a close look at the team composed of the “__isset() “ and “__unset()” magic functions, so you can quick grasp how to use them in concrete situations.

Now, let’s get rid of the rather boring theory and continue discovering the real power behind working with magic functions in PHP 5. Let’s go!

{mospagebreak title=Review: the set and get magic functions}

Before I start explaining how to use the “__isset()” and “__unset()” magic functions, it would be helpful to reintroduce the example developed in the preceding tutorial. It demonstrated how to use the “__set()” and “__get()” methods for overloading a basic user-related class.

Having said that, here’s the complete source code corresponding to that sample class:

class User

{

// constructor (not implemented)

public function _construct(){}

 

// set undeclared property

function __set($property, $value)

{

$this->$property = $value;

}

 

// get defined property

function __get($property)

{

if (isset($this->$property))

{

return $this->$property;

}

}

}

I’m not going to explain again how the above “User” class does its business, since this was already covered in depth in the previous tutorial. Instead, I’m going create a short script that shows how to dynamically add some properties to the class in question, and to retrieve their respective values.

So, basically the script that performs all of these interesting tasks looks like this:

// example of usage of ‘User’ class with property overloading

$user = new User();

$user->fname = ‘Alejandro';

$user->lname = ‘Gervasio';

$user->email = ‘alejandro@mydomain.com';

$user->address = ‘My address 1234′;

 

// display user data

echo ‘First Name: ‘ . $user->fname . ‘ Last Name: ‘ . $user->lname . ‘ Email: ‘ . $user->email . ‘ Address: ‘ . $user->address;

/*

displays the following

First Name: Alejandro Last Name: Gervasio Email: alejandro@mydomain.com Address: My address 1234

*/

There you have it. By giving a concrete implementation to the “__set()” and “__get()” functions, it is possible to add new properties to the “User” class without having to declare them explicitly. Of course, in doing so the whole source code of the class might become less readable. Unfortunately, that’s the price that must be to paid when it comes to overloading class properties.

So far, so good. At this stage you’ve hopefully become familiar with using the handy “__set()” and “__get()” magic functions. Given the inherent flexibility these function provide, it’s also feasible to modify their implementation and give them the capacity to determine which properties of a class to create and which to avoid creating, by means of a simple control mechanism.

Assuming that you’re interested in learning how to build this checking mechanism, in the section to come I’m going to change the definition of the previous “User” class so it can restrict the creation of undeclared properties according to a predefined schema.

To see how this will be accomplished, click on the link below and keep reading.

{mospagebreak title=Determining what properties to create within a class}

In accordance with the concepts deployed in the preceding section, I’m going to change the implementation of the “__set()” method defined within the “User” class that we learned before, so it can restrict the creation of undeclared properties only to “fname,” “lname” and “email.” If a script tries to create a property other than the ones specified, the process will simply fail gracefully.

Now that I’ve explained the modifications that I plan to introduce to the “User” class, please pay attention to its brand new signature, which is as follows:

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)

{

if (isset($this->$property))

{

return $this->$property;

}

}

}

See how easy it is to give the above “User” class the ability to control which properties to create and which to refrain from creating? I guess you do! As I explained before, in this particular example, the class will only allow the creation of the “fname,” “lname” and “email” properties, so any attempt to create a different one will be unsuccessful.

In order to demonstrate more clearly how this example class now works, below I coded a script that illustrates what happens when a “forbidden” property is added to an instance of the “User” class. Here it is:

// example on using ‘User’ class with property overloading

$user = new User();

$user->fname = ‘Alejandro';

$user->lname = ‘Gervasio';

$user->email = ‘alejandro@mydomain.com';

// this property won’t be created

$user->address = ‘My address 1234′;

 

// display user data

echo ‘First Name: ‘ . $user->fname . ‘ Last Name: ‘ . $user->lname . ‘ Email: ‘ . $user->email . ‘ Address: ‘ . $user->address;

/*

displays the following

First Name: Alejandro Last Name: Gervasio Email: alejandro@mydomain.com Address:

*/

That was pretty interesting, right? As you can see above, the line that attempts to create a new property called “address” will fail because of the control implemented within the “__set()” method.

While rather simplistic, this example should give you a clearer idea of how powerful these magic functions can be when it comes to simplifying the creation and assignment of properties to a given class.

Well, having finished this brief introduction to using the “__set()” and “__get()” functions, it’s time to continue exploring other magic methods bundled with PHP 5. So, with that idea in mind, in the last segment of this article I’m going to discuss the use of the complementary “__isset()” and “__unset()” functions.

To learn more about these functions, please go ahead and read the following section. It’s only one click away.

{mospagebreak title=Introducing the isset and unset magic functions}

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';

isset($user->email);

/*

display the following

Checking if property email has been set…

*/

 

unset($user->email);

/*

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!

[gp-comments width="770" linklove="off" ]

chat sex hikayeleri Ensest hikaye