Robert, thanks very much for your detailed explanation. This makes
sense as I've wrapped my brain around protected and private property
types.
I think I'll be leaving them as public members for performance
reasons. Down the road, I may add special handling for certain members
that should be private and protected. The __sleep() and __wake()
function should come in handy for that.
Thanks again.
~Ted
On 27-May-08, at 8:27 AM, Robert Cummings wrote:
On Tue, 2008-05-27 at 11:00 +0530, Chetan Rane wrote:
Can you please example code because I simulated the similar thing
and it works fine here
<?php
class A {
protected $x = '12345';
protected $y = '12345';
function __construct() {
}
public function Hello(){
echo Hello2;
}
}
echo "<pre>";
$a = new A();
echo serialize($a);
print_r($a);
print_r(unserialize(serialize($a)));
?>
O/P :
O:1:"A":2:{s:4:"�*�x";s:5:"12345";s:4:"�*�y";s:5:"12345";}A
Object
(
[x:protected] => 12345
[y:protected] => 12345
)
A Object
(
[x:protected] => 12345
[y:protected] => 12345
)
I think that’s perfectly fine.
You didn't read his problem correctly. Try the following:
foo.php4 (Run via PHP4)
---------------------------------------------------------
<?php
class Foo
{
var $aaa = 'aaa4';
var $bbb = 'bbb4';
var $ccc = 'ccc4';
}
echo serialize( new Foo() );
?>
Now using the output from the above script paste it into the $foo4 var
in the following script (it should be as shown in my example below):
foo.php5 (Run via PHP5)
---------------------------------------------------------
<?php
class Foo
{
protected $aaa = 'aaa5';
protected $bbb = 'bbb5';
protected $ccc = 'ccc5';
}
$foo4 =
'O:3:"foo":3:{s:3:"aaa";s:4:"aaa4";s:3:"bbb";s:4:"bbb4";s:3:"ccc";s:
4:"ccc4";}';
$foo4 = unserialize( $foo4 );
var_dump( $foo4 );
?>
The reason this happens is because serialize tracks the var type. In
the
PHP4 case, no var type is possible and so no var type is stored. As
such
the unserialize function does what PHP code does... it presumes
public.
However, at a low level within the PHP binary, there is no check to
see
if the property exists in another form. For instance, to unserialize,
the unserialize code instantiates a new Foo object when it encounters
the Foo instance in the serialized data stream. When it is
instantiated
the protected members are initialized. But protected members are
stored
differently internally than public members, and so, I presume for
speed,
PHP didn't bother testing for the existence of both, but rather gives
one precedence over the other when a property is requested. So
happily,
the different variable types are co-existing. Some pbrief testing
shows
that private members are getting precedence. So this is obviously an
issue for all the old serialized data once you promote the var type to
protected.
So what CAN you do??
You can choose not to change the properties to protected.
You can use use an import technique to import the serialized data and
resave it in the PHP5 format:
foo.php5 (Run via PHP5)
---------------------------------------------------------
<?php
class Foo
{
protected $aaa = 'aaa5';
protected $bbb = 'bbb5';
protected $ccc = 'ccc5';
static function importFromPhp4( $serialized )
{
$array = (array)unserialize( $serialized );
$import = new Foo();
$import->aaa = $array['aaa'];
$import->bbb = $array['bbb'];
$import->ccc = $array['ccc'];
return $import;
}
}
$foo4 =
'O:3:"foo":3:{s:3:"aaa";s:4:"aaa4";s:3:"bbb";s:4:"bbb4";s:3:"ccc";s:
4:"ccc4";}';
$foo = Foo::importFromPhp4( $foo4 );
var_dump( $foo );
?>
That should solve the problem.
Cheers,
Rob.
--
http://www.interjinn.com
Application and Templating Framework for PHP
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php