vendredi 23 septembre 2016

PHP strange behaviour: ReflectionClass::isCloneable() calls destructor?

Consider following code:

class A {
    function __construct() { echo __FUNCTION__ . "\n"; }
    function __destruct() { echo __FUNCTION__ . "\n"; }
}

$a = new A();
$c = new ReflectionClass($a);
$c->isCloneable();

Just instancing new class, and checking if it is cloneable using reflection.

The output is unexpected:

__construct
__destruct
__destruct

Why __destruct called two times? After looking into PHP source code, it seems that in ext/php_reflection/reflection.c ZEND function isCloneable occurs call to zval_dtor(&obj) when reflected object has no __clone() method defined. So, adding __clone() to class fixes the double destructor issue. What's that? Bug in PHP?

P.S. Tested in PHP 5.4 and 5.6.





Aucun commentaire:

Enregistrer un commentaire