Hi,

On Wed, Feb 22, 2017 at 11:18 PM, Michał <aaat...@o2.pl> wrote:

> 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?
>

This isn't just a PHP 7 work-around for null types, it has existed since
object type hints were introduced and is very widespread.

Cheers,
Andrey.

Reply via email to