Back to questions
JuniorTheoryCommon
What does the program print?
<?php
class Counter
{
public int $value = 0;
}
$a = new Counter();
$b = new Counter();
$a->value += 5;
echo $a->value, ' ', $b->value, "\n";5 0
- Expecting
5 5, as if both variables held one counter: eachnewbuilds its own object with its own$value, so changing$aleaves$bat0. - Expecting an object to be fixed once
newhas built it: apublicproperty can be changed at any time, and$a->value += 5makes it5.
- How would you change
Counterso that all its objects share one counter? - What does
$a === $breturn here, and why?