CKTIS / PHP
Keyword clone
Create a new object using a shallow property copy and then invoke __clone() if defined.
Version: PHP 8.4; repository runtime, production runtime not attested
Syntax
clone $originalCreate a new object using a shallow property copy and then invoke __clone() if defined. An object stored inside a copied property is still shared unless the hook explicitly clones it. Reference-valued properties also retain their reference bindings.
class Batch {public function __construct(public object $item) {}}
$a=new Batch((object)["n"=>1]); $b=clone $a; $b->item->n=2;
echo json_encode([$a===$b,$a->item===$b->item,$a->item->n]);
[false,true,2]
Examples
<?php
class Batch {public function __construct(public object $item) {}}
$a=new Batch((object)["n"=>1]); $b=clone $a; $b->item->n=2;
echo json_encode([$a===$b,$a->item===$b->item,$a->item->n]);
Result
[false,true,2]