Code Inspection: Old style constructor
Configure inspections: Settings | Editor | Inspections
Show intention actions: AltEnter
Reports old-style constructor declarations (ClassName()
) and suggests replacing them with new-style constructors (__construct()
).
Prior to PHP 8.0, old-style constructor syntax used in the classes in the global namespace is treated as deprecated and will result in an E_DEPRECATED
error. If both a __construct()
and a ClassName()
method are defined, __construct()
will be called.
In namespaced classes, or any classes as of PHP 8.0, a ClassName()
method never has any special meaning and will never be called as a constructor. As a result, the inspection does not report such cases if the PHP language level is set to 8.0 or later.
See Constructors and destructors (php.net) for details.
Using old-style constructors is highly discouraged. In PHP 7, such usages will raise an E_DEPRECATED
notice. In future PHP versions, support for old-style constructors will be removed. In the following example, the constructor for class Example
is defined in the old form.
Old-style constructor
class Example { public function Example() {}}
New-style constructor
class Example { public function __construct() {}}
Place the caret at the highlighted line and press AltEnter or click
.
Click the arrow next to the inspection you want to suppress and select the necessary suppress action.