Returning A Value From A Constructor Is Deprecated

A constructor’s return value was always ignored by PHP, silently. In PHP 8.6, returning any value, other than not returning at all, from __construct() emits a deprecation notice. The same applies to __destruct().

PHP code

<?php

class x {
    public function __construct() {
        return 5;
    }
}

new x();
print "done\n";

?>

Before

done

After

PHP Deprecated:  Returning a value from a constructor is deprecated

Deprecated: Returning a value from a constructor is deprecated
done

PHP version change

This behavior was deprecated in 8.6

This behavior changed in

See Also

Error Messages