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

Determining what properties to create within a class - 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.

TABLE OF CONTENTS:
  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
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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.



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

blog comments powered by Disqus
   

PHP ARTICLES

- 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...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 6 - Follow our Sitemap

Dev Shed Tutorial Topics: