The Magic __set_state Method
|
|
|
Click to rate: |
|
|
|
70 votes / avg. rating 23.07%
|
|
It is fairly self-evident how to use most of the magic methods of PHP 5.
However, it is not quite so apparent how to use the __set_state method introduced in PHP 5.1.
In this article, Peter lavin delves into this elusive magic method to
show you how to use this gem in some really 'classy' object oriented PHP.
In PHP 4 there are only two magic methods, __serialize and __unserialize. PHP 5 .0 defines a number of new magic methods the most well-known being the constructor and destructor. Apart from beginning with a double underscore, magic methods in PHP are (usually) invoked indirectly.
While it's possible to call any magic method directly, the only sensible direct call to a magic method is a call to the parent constructor and then only from within the child constructor i.e. parent::__construct(). For instance, when a __construct method is defined, it is called whenever an object is created. If we have a class named A, then the __construct method is indirectly invoked by the code:
Why have Magic Methods?The reasons for the existence of magic methods vary. The __toString method, for example, could perhaps best be described as merely a convenience. By defining a __toString method, we can control how an object is displayed when it is used with the echo or print functions. However, the same effect could be achieved by defining a display method and calling it whenever we wanted to display an object. At the opposite end of the spectrum is a magic method like __clone. This method is absolutely necessary in order to control what happens when an object is copied. Without it, copying an aggregate object would always result in a shallow copy -- often not the desired effect.The Newest Magic MethodThe newest magic method, __set_state first appears in PHP 5.1.
Like other magic methods it is invoked in the background,
in this case by a call to var_export. In this respect it bears a strong resemblance
to the __toString method. As we have seen, the __toString method is also
invoked by a PHP function call. It's clear that the string representation
of a simple data type such as an integer should simply be it's value. If we have assigned the value of 5 to the variable $x,
then we expect to see that value when the variable is echoed.
But an object is a complex data type. Defining a __toString method
makes sense because the string representation of an object is not a
straightforward matter. The need for a magic __set_state method like
the __toString method, arises from the fact that objects are complex data types.
Why A __set_state MethodTo better understand the need for a magic __set_state method, let's look at what var_export does. var_export is one of numerous variable handling functions and it is very similar to var_dump and print_r. These functions are all typically used for debugging purposes and return the same information in slightly different formats. The principal difference is that var_export returns valid, parseable PHP code. Here's what the php.net site has to say:
“var_export() gets structured information about the given variable. It is similar to var_dump with one exception: the returned representation is valid PHP code.” Using var_exportWhen using var_export with primitives or even with arrays, it is pretty evident how variables should be represented. Let's have a quick look at var_export when used with non-objects.
Take a simple array such as:
$array = array('a', 'b', 'c');
When passed to var_export like so:
<code php>
The parseable code:
<code php>
array ( 0 => 'a', 1 => 'b', 2 => 'c', ) is displayed -- the exact PHP syntax for creating an array..
The var_export function also accepts an optional Boolean second parameter. When set to true, a copy of the exported variable can be created instead of just being echoed. We can copy the $array variable defined above in the following way:
When executed this code creates the variable $newarray, a copy of $array. Dumping the newly created variable will confirm this.
You might expect that because var_export works with an array it might well work with an object but not so. What should the parseable representation of an object be? Arrays are limited to being either associative or numerical but objects can take many varied forms. We noted earlier that because an object is a complex data type the string representation is problematic. The bug report at http://bugs.php.net/bug.php?id=29361 highlights the problem with objects and var_export. When used with an object (in this case as an element of an array), the return value of var_export (PHP 4.3.8) is as follows:
0 =>
class myclass {
var $myProperty = 'myValue';
},
); This might well be an adequate representation of the object but it isn't parseable code.
 |
|
 |
|
Tags:
shit, damn, poop, hein, magic, ll, test, fucker, ghey, dodod, varexport, setstate method, magic methods, sailboat, fnord, ripe for spamming, tetragrammatolalia
Tags Help
Tags are keywords associated with a web page that help classifying information. You can find a good explanation here.
To add one or more tags to this page, simply enter them below (separate them with a comma) and hit enter or click on the "Go" button.
|
|
 |
|
 |
|
Index
|