note 87404 modified in language.oop5.magic by danbrown
| From: | danbrown@php.net | Date: | Thu, 04 Dec 2008 01:32:36 +0000 |
| Subject: | note 87404 modified in language.oop5.magic by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-147603@lists.php.net to get a copy of this message | ||
Be very careful to define __set_state() in classes which inherit from a parent using it, as the
static __set_state() call will be called for any children. If you are not careful, you will end up
with an object of the wrong type. Here is an example:
<?php
class A
{
public $var1;
public static function __set_state($an_array)
{
$obj = new A;
$obj->var1 = $an_array['var1'];
return $obj;
}
}
class B extends A {
}
$b = new B;
$b->var1 = 5;
eval('$new_b = ' . var_export($b, true) . ';');
var_dump($new_b);
/*
object(A)#2 (1) {
["var1"]=>
int(5)
}
*/
?>
--was--
Be very careful to define __set_state() in classes which inherit from a parent using it, as the
static __set_state() call will be called for any children. If you are not careful, you will end up
with an object of the wrong type. Here is an example:
class A
{
public $var1;
public static function __set_state($an_array)
{
$obj = new A;
$obj->var1 = $an_array['var1'];
return $obj;
}
}
class B extends A {
}
$b = new B;
$b->var1 = 5;
eval('$new_b = ' . var_export($b, true) . ';');
var_dump($new_b);
/*
object(A)#2 (1) {
["var1"]=>
int(5)
}
*/
http://php.net/manual/en/language.oop5.magic.php