Skip to content

DeprecatedConstructorStyle ​

error on by default quick-fix

Group: Language level migration · PhpStorm name: DeprecatedConstructorStyleInspection

A method named after its class used to act as the constructor (PHP 4 style). This form is deprecated since PHP 7.0 and no longer a constructor in PHP 8.0; the method should be called __construct.

Example ​

php
<?php
class Ledger
{
    private $rows;

    public function Ledger(array $rows)
    {
        $this->rows = $rows;
    }
}

abstract class Shape
{
    abstract protected function Shape($sides);
}

class Counter
{
    public static function Counter() { return new self(); }
}

class Wallet
{
    public function __construct($amount) {}
    public function Wallet($amount) { $this->__construct($amount); }
}

class Gauge
{
    public function gauge() {}
}

trait Logger
{
    public function Logger() {}
}

interface Printer
{
    public function Printer();
}
php
<?php
class Ledger
{
    private $rows;

    public function __construct(array $rows)
    {
        $this->rows = $rows;
    }
}

abstract class Shape
{
    abstract protected function __construct($sides);
}

class Counter
{
    public static function Counter() { return new self(); }
}

class Wallet
{
    public function __construct($amount) {}
    public function Wallet($amount) { $this->__construct($amount); }
}

class Gauge
{
    public function __construct() {}
}

trait Logger
{
    public function Logger() {}
}

interface Printer
{
    public function Printer();
}

Reported:

  • line 6: Class 'Ledger' uses an old-style constructor; rename it to __construct.
  • line 14: Class 'Shape' uses an old-style constructor; rename it to __construct.
  • line 30: Class 'Gauge' uses an old-style constructor; rename it to __construct.

Configure ​

In custos.json:

json
{
  "rules": {
    "DeprecatedConstructorStyle": {
      "enabled": false
    }
  }
}

Suppress ​

Before the statement or declaration (or the first statement of the file), either of:

php
// @custos-ignore DeprecatedConstructorStyle

/**
 * @noinspection DeprecatedConstructorStyleInspection
 */

Released under the MIT License. Rule catalogue modelled on Php Inspections (EA Extended); independent clean-room implementation.