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!
blog comments powered by Disqus |
|
|
|
|
|
|
|