You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
clone $obj lowers to TYPE_CLONE and copies the object shell, but __clone() is never called. Zend runs the magic method after the shallow copy so objects can deep-copy or adjust state.
Today VM only does cloneShallow():
caseOpCode::TYPE_CLONE:
$result=$frame->scope[$op->arg1];
$src=$frame->scope[$op->arg2]->resolveIndirect();
if (Variable::TYPE_OBJECT!==$src->type) {
thrownew\LogicException('clone requires an object');
}
$result->object($src->toObject()->cloneShallow());
break;
Category
languageProblem
clone $objlowers toTYPE_CLONEand copies the object shell, but__clone()is never called. Zend runs the magic method after the shallow copy so objects can deep-copy or adjust state.Today VM only does
cloneShallow():php-src reference
Zend/zend_objects.c—zend_objects_clone_obj(),zend_std_clone_object()Zend/zend_compile.c— clone opcode emits call to__clonewhen presentRepro
Zend reference:
php -r 'class C { public int $x = 1; public function __clone() { $this->x = 2; } } $a = new C(); $b = clone $a; echo $b->x;'Scope (this repo)
lib/Compiler.php— optionalTYPE_METHOD_CALLafter clone when class defines__clonelib/VM.phpTYPE_CLONE,lib/VM/ObjectEntry.phplib/JIT.php/lib/JIT/Builtin/Type/Object_.php— mirror VM hook orderlib/AOT/if clone op used on self-host pathtest/compliance/cases/oop/clone_magic.phptDone when
2underbin/vm.php,bin/jit.php(or documented JIT deferral in matrix), and AOT fixture if clone used in inventory.php script/capability-syntax.phprow for__cloneupdated (or new row).__clone.Notes
__clonedispatch is separate from JIT: clone expression lowering (object copy) #1223 (clone JIT opcode exists); this issue is magic method semantics.__clonechaining (parent::__clone()) follows normal method dispatch once hook runs.