For consistency I'm specifying return types since PHP 7.1, for all methods, including magic ones like __toString, and even when the implicit return type is void like with __unserialize():
class a {
function __toString() : string {}
function __unserialize ( array $data ) : void {}
function __wakeup() : void {}
}
When I try the same for constructors and destructors, like this:
class a {
function __construct() : void {}
function __destruct() : void {}
function __clone() : void {}
}
PHP yields Fatal errors:
Constructor a::__construct() cannot declare a return type
Destructor a::__destruct() cannot declare a return type
Clone method a::__clone() cannot declare a return type
The only thing I can do right now is to specify the implicit return type in a docblock like this:
/**
* @return void (implicit)
*/
It puzzles me why, because other predefined methods do support an explicit return type. I couldn't find anything about this deviation in the docs, or in the RFC.
How can I specify the return type void for constructors and destructors? If it isn't possible in PHP 7, will it become possible in PHP 8 ?