PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > php.general > potentially __sleep() bug
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
potentially __sleep() bug

Réponse
 
LinkBack Outils de la discussion
Vieux 30/01/2008, 02h52   #1
Nathan Nobbe
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut potentially __sleep() bug

all,

i was playing around w/ some object serialization tonight during
further exploration of spl and i stumbled on what appears to be a
bug in the behavior of the __sleep() magic method.

here is the pertinent documentation on the method
..is supposed to return an array with the names of all variables
of that object that should be serialized.

so, the idea is, *only* the instance variables identified in the array
returned are marked for serialization.
however, it appears all instance variables are being serialized no matter what.
see the reproducible code below. ive run this on 2 separate php5
boxes, one w/ 5.2.5, another w/ a 5.2.something..

<?php
class A {
public $a1 = 'a1';
public $a2 = 'a2';
public $a3 = 'a3';

public function __sleep() {
echo __FUNCTION__ . PHP_EOL;
return array('a1', 'a2');
}
}

var_dump(unserialize(serialize(new A())));
?>

this is what i get despite having marked only member variables 'a',
and 'b' for serialization.

__sleep
object(A)#1 (3) {
["a1"]=>
string(2) "a1"
["a2"]=>
string(2) "a2"
["a3"]=>
string(2) "a3"
}

consensus ?

-nathan
  Réponse avec citation
Vieux 30/01/2008, 03h24   #2
ZeldorBlat
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: potentially __sleep() bug

On Jan 29, 8:52 pm, quickshif...@gmail.com ("Nathan Nobbe") wrote:
> all,
>
> i was playing around w/ some object serialization tonight during
> further exploration of spl and i stumbled on what appears to be a
> bug in the behavior of the __sleep() magic method.
>
> here is the pertinent documentation on the method
> ..is supposed to return an array with the names of all variables
> of that object that should be serialized.
>
> so, the idea is, *only* the instance variables identified in the array
> returned are marked for serialization.
> however, it appears all instance variables are being serialized no matter what.
> see the reproducible code below. ive run this on 2 separate php5
> boxes, one w/ 5.2.5, another w/ a 5.2.something..
>
> <?php
> class A {
> public $a1 = 'a1';
> public $a2 = 'a2';
> public $a3 = 'a3';
>
> public function __sleep() {
> echo __FUNCTION__ . PHP_EOL;
> return array('a1', 'a2');
> }
>
> }
>
> var_dump(unserialize(serialize(new A())));
> ?>
>
> this is what i get despite having marked only member variables 'a',
> and 'b' for serialization.
>
> __sleep
> object(A)#1 (3) {
> ["a1"]=>
> string(2) "a1"
> ["a2"]=>
> string(2) "a2"
> ["a3"]=>
> string(2) "a3"
>
> }
>
> consensus ?
>
> -nathan


When you unserialize it $a3 gets the default value you specified in
the class declaration. Try this:

$a = new A();
$a->a3 = 'foo';
var_dump($a);
var_dump(unserialize(serialize($a)));

See the difference?
  Réponse avec citation
Vieux 30/01/2008, 14h06   #3
Anup Shukla
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] potentially __sleep() bug

Nathan Nobbe wrote:
> all,
>
> i was playing around w/ some object serialization tonight during
> further exploration of spl and i stumbled on what appears to be a
> bug in the behavior of the __sleep() magic method.
>
> here is the pertinent documentation on the method
> ..is supposed to return an array with the names of all variables
> of that object that should be serialized.
>
> so, the idea is, *only* the instance variables identified in the array
> returned are marked for serialization.
> however, it appears all instance variables are being serialized no matter what.
> see the reproducible code below. ive run this on 2 separate php5
> boxes, one w/ 5.2.5, another w/ a 5.2.something..
>
> <?php
> class A {
> public $a1 = 'a1';
> public $a2 = 'a2';
> public $a3 = 'a3';
>
> public function __sleep() {
> echo __FUNCTION__ . PHP_EOL;
> return array('a1', 'a2');
> }
> }
>
> var_dump(unserialize(serialize(new A())));
> ?>
>
> this is what i get despite having marked only member variables 'a',
> and 'b' for serialization.
>
> __sleep
> object(A)#1 (3) {
> ["a1"]=>
> string(2) "a1"
> ["a2"]=>
> string(2) "a2"
> ["a3"]=>
> string(2) "a3"
> }
>
> consensus ?
>


To check if __sleep is proper, you should be doing
var_dump(serialize(new A()));

unserialize'ing effectively also does a __wakeup()

This should give a clearer picture

<?php
class A {
public $a1 = 'a1';
public $a2 = 'a2';
public $a3 = null;

public function __construct(){
$this->a3 = 'a3';
}

public function __sleep() {
echo __FUNCTION__ . PHP_EOL;
return array('a1', 'a2');
}
}

var_dump(unserialize(serialize(new A())));
?>

__sleep
object(A)#1 (3) {
["a1"]=>
string(2) "a1"
["a2"]=>
string(2) "a2"
["a3"]=>
NULL
}

============= and ======================

<?php
class A {
public $a1 = 'a1';
public $a2 = 'a2';
public $a3 = null;

public function __construct(){
$this->a3 = 'a3';
}

public function __sleep() {
echo __FUNCTION__ . PHP_EOL;
return array('a1', 'a2', 'a3');
}
}

var_dump(unserialize(serialize(new A())));
?>

__sleep
object(A)#1 (3) {
["a1"]=>
string(2) "a1"
["a2"]=>
string(2) "a2"
["a3"]=>
string(2) "a3"
}

--
Regards,
Anup Shukla
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 02h36.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,14311 seconds with 11 queries