A method named like its class is no longer run as the constructor

tx0/php:class-named-methodrecommendedwarningreliability/api-misusedepth fastbug

What it finds

A method named like its class, in a class without __construct: PHP 4 ran such a method as the constructor, and PHP 8 no longer does, so code written that way leaves new objects uninitialized. Classes in a namespace never had such constructors and are left alone.

From the rule's source, class_named_method.rs.

Example from the rule's tests
<?phpclass Widget{    private $size;     public function Widget($size)tx0 reports: Widget is named like its class but is not a constructor    {        $this->size = $size;    }} class Gadget{    public function __construct()    {    }     public function gadget()    {    }}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/php:class-named-method" = "warning"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/php:class-named-method] reason="..."

tx0 explain tx0/php:class-named-method shows the same in the terminal. Found a false positive? Report it.