Back to questions
JuniorTheoryCommon
What does the program print?
<?php
class Point
{
public function __construct(public int $x) {}
}
$a = new Point(1);
$b = $a;
$c = clone $a;
$b->x = 5;
echo $a->x, ' ', $b->x, ' ', $c->x, "\n";5 5 1
- Expecting
1 5 1, as if$b = $acopied the object like an array: it copies only the object handle, so$aand$blead to one object. - Expecting
cloneto give one more name for the same object: it builds a second object with copies of the properties, so the later write misses$c.
- What do
$a === $band$a === $creturn here? - If a property of
Pointheld another object, wouldclonecopy that object too?