Old Style Constructor¶
Since PHP 5, the constructor method of a class was the eponymous method: the method with the same name as the class.
In PHP 7, this feature was deprecated in favor of using the __construct
. During that phase, __construct
had priority over the eponymous function, but the latter was still used in case of fallback, for backward compatibility.
In PHP 8, the eponymous method is now a normal method.
PHP code¶
<?php
class X {
function X() {
echo __METHOD__;
}
}
var_dump(new X());
?>
Before¶
PHP Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; X has a deprecated constructor in /codes/oldStyleConstructor.php on line 3
Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; X has a deprecated constructor in /codes/oldStyleConstructor.php on line 3
X::Xobject(X)#1 (0) {
}
After¶
object(X)#1 (0) {
}
PHP version change¶
This behavior was deprecated in 7.0
This behavior changed in 8.0