Back to questions
JuniorTheoryCommon
What does the program print?
<?php
class User
{
public static int $count = 0;
public function __construct() { self::$count++; }
}
new User();
new User();
echo User::$count, "\n";2
- Expecting
1, as if each object had its own counter: astaticproperty belongs to the class, so both constructors add to the same$count. - Expecting
new User();without a variable to do nothing:newalways builds the object and runs its constructor, the object is only not kept.
- What would
echo User::$countdo if$countwere an ordinary property, not astaticone? - Why can a
staticproperty make two tests depend on each other?