From:             uwendel at mysql dot com
Operating system: 
PHP version:      5.3CVS-2008-03-11 (CVS)
PHP Bug Type:     PDO related
Bug description:  PDO::FETCH_SERIALIZE calls __construct()

Description:
------------
There seems to be very few documentation about PDO::FETCH_SERIALIZE in the
PHP manual but playing the guessing game from the code it seems that this
feature aims to support SPL/Serialize interface. As I'm not sure about the
purpose of PDO::FETCH_SERIALIZE I'm not sure if the following is a bug or
not. However, it seems to me that PDO::FETCH_SERIALIZE unintentionally
calls __construct().

One of the main ideas behind SPL/Serialize interface seems to be that for
unserialization the constructor of a class does not get called. The
constructor of a class has a different meaning than a helper function like
unserialize() and thus should not be called automatically. Let's check:

class myclass implements Serialize {
  public function __construct() {
    printf("%s()\n", __METHOD__);
  }
  public function serialize() {
    printf("%s()\n", __METHOD__);
    return "any data from serialize()";
  }
  public function unserialize($dat) {
    printf("%s(%s)\n", __METHOD__, var_export($dat, true));
  }
}

$obj1 = new myclass()     
  ---> myclass::__construct()
$tmp  = serialize($obj1)    
$obj2 = unserialize($tmp) 
  ---> myclass::unserialize('any data from serizalize()')

__construct() gets called only once for object creation but not again
during unserialization. Let's try that with PDO:

[...]
$stmt = $db->query("SELECT dat FROM test");
$rows = $stmt->fetchAll(PDO::FETCH_CLASS|PDO::FETCH_SERIZALIZE,
"myclass");
  --> myclass::unserialize("data from DB")
  --> myclass::__construct()
[...]

PDO first calls unserialize() as its supposed to do. But then it also
calls __construct() which is against the idea of the Serialize interface
not to call the constructor automatically during unserialization.

Reproduce code:
---------------
sapi/cli/php -r '$db = new PDO("sqlite:/tmp/foo"); $db->exec("DROP TABLE
test"); $db->exec("CREATE TABLE test(dat VARCHAR(100))"); $db->exec("INSERT
INTO test(dat) VALUES (\"Data from DB\")"); class myclass implements
Serializable { public function __construct() { printf("%s()\n",
__METHOD__); } public function serialize() { return "any data from
serizalize()"; } public function unserialize($dat) { printf("%s(%s)\n",
__METHOD__, var_export($dat, true)); }} $stmt = $db->query("SELECT * FROM
test"); var_dump($stmt->fetchAll(PDO::FETCH_CLASS|PDO::FETCH_SERIALIZE,
"myclass")); $obj = new myclass();
var_dump(unserialize(serialize($obj)));'
myclass::unserialize('Data from DB')
myclass::__construct()
array(1) {
  [0]=>
  object(myclass)#3 (0) {
  }
}
myclass::__construct()
myclass::unserialize('any data from serizalize()')
object(myclass)#4 (0) {
}



-- 
Edit bug report at http://bugs.php.net/?id=44409&edit=1
-- 
Try a CVS snapshot (PHP 5.2): 
http://bugs.php.net/fix.php?id=44409&r=trysnapshot52
Try a CVS snapshot (PHP 5.3): 
http://bugs.php.net/fix.php?id=44409&r=trysnapshot53
Try a CVS snapshot (PHP 6.0): 
http://bugs.php.net/fix.php?id=44409&r=trysnapshot60
Fixed in CVS:                 http://bugs.php.net/fix.php?id=44409&r=fixedcvs
Fixed in release:             
http://bugs.php.net/fix.php?id=44409&r=alreadyfixed
Need backtrace:               http://bugs.php.net/fix.php?id=44409&r=needtrace
Need Reproduce Script:        http://bugs.php.net/fix.php?id=44409&r=needscript
Try newer version:            http://bugs.php.net/fix.php?id=44409&r=oldversion
Not developer issue:          http://bugs.php.net/fix.php?id=44409&r=support
Expected behavior:            http://bugs.php.net/fix.php?id=44409&r=notwrong
Not enough info:              
http://bugs.php.net/fix.php?id=44409&r=notenoughinfo
Submitted twice:              
http://bugs.php.net/fix.php?id=44409&r=submittedtwice
register_globals:             http://bugs.php.net/fix.php?id=44409&r=globals
PHP 4 support discontinued:   http://bugs.php.net/fix.php?id=44409&r=php4
Daylight Savings:             http://bugs.php.net/fix.php?id=44409&r=dst
IIS Stability:                http://bugs.php.net/fix.php?id=44409&r=isapi
Install GNU Sed:              http://bugs.php.net/fix.php?id=44409&r=gnused
Floating point limitations:   http://bugs.php.net/fix.php?id=44409&r=float
No Zend Extensions:           http://bugs.php.net/fix.php?id=44409&r=nozend
MySQL Configuration Error:    http://bugs.php.net/fix.php?id=44409&r=mysqlcfg

Reply via email to