Home arrow PHP arrow Page 3 - Magic Functions in PHP 5

Property overloading in action - PHP

Itís not breaking news that the release of PHP 5 drastically changed the way that many developers build their web-based programs. The incorporation of a much more robust object model, along with the introduction of native exceptions, type hinting and so forth (add your own improvement to the list) has given the language the maturity that we see in it today. This seven-part article series will explain an important new feature: magic functions.

  1. Magic Functions in PHP 5
  2. Get and set functions: a quick overview
  3. Property overloading in action
  4. The set and get methods in action
By: Alejandro Gervasio
Rating: starstarstarstarstar / 12
May 26, 2009

print this article



In the previous segment, I created a basic "User" class, whose API allowed us to easily assign and retrieve the values of its declared properties. However, as I expressed earlier, it's possible to shorten its  definition and make it more "dynamic" simply by concretely implementing the "__set()" and "__get()" methods.

To demonstrate this concept a bit more, I'm going to redefine this sample class to allow us not only to create new class properties at run time, but retrieve their respective values with extreme ease.

Now that you know what I'm going to do in the next few lines, please examine the modified version of the "User" class. It now looks like this:

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;




Now, suddenly things are much more interesting than in the example shown in the previous section. As you can see above, the "User" class is only comprised of two methods -- the corresponding "__set()" and "__get()" functions. And in this specific situation, they have been given a concrete implementation.

In the first case, the "__set()" function will create an undeclared property and assign a value to it, while in the last case, its counterpart "__get()" will retrieve this value if the property has been previously set.

The implementation of these two methods permits us to overload properties in a truly simple way. It also makes the class's source code much shorter and more compact. But, as with everything in life, this process has some disadvantages that must be taken into account. First, and most importantly, the lack of an explicit declaration for each property of the class makes the code harder to read and follow. Second, it's practically impossible for an IDE to keep track of those properties for either commenting or documenting them.

Logically, this is something that must be evaluated consciously when overloading the properties of a class. Its implementation depends strongly on the context where it will be used.

Now, returning to the previous example class, given that it's already been modified, it's time to give it a try, right? So, with that idea in mind, in the next section I'm going to create an example that will show how to assign undeclared properties to it, and how to retrieve their values.

Don't waste more time; read the following segment. It's only one click away

>>> 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: