Hello PHP internals,
Scalar types declarations were introduced in PHP 7.0 but it was not possible to pass null as a default value to function/method. It was finally done by a small workaround described in documentation as "The declaration can be made to accept NULL values if the default value of the parameter is set to NULL".

In PHP 7.1 and it's new feature - nullable types - this hack is, in my opinion, completely unnecessary. It breaks valid type-hinting, is not logical for advanced programmers, and may cause potential bugs. According to docs "Type declarations allow functions to require that parameters are of a certain type at call time. If the given value is of the incorrect type, then an error is generated: in PHP 5, this will be a recoverable fatal error, while PHP 7 will throw a TypeError exception.". Following this, first line of code should throw error - but it does not, because of 7.0 work-around.

a(string $test=null) // incorrect type, should throw error
a(string $test='test') // valid type, ok
a(?string $test=null) // valid type, ok
a(?string $test='test') // valid type, ok

There is no reason, except of backwards compatibility, to keep this behaviour. It's too late to change this in 7.1, maybe even in 7.2. But, what is Your opinion? Should it be preserved or fixed?

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to