php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #80948 Optional parameter can be null oven when type hint says no
Submitted: 2021-04-12 08:25 UTC Modified: 2021-04-12 08:45 UTC
From: andre at webkr dot de Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 8.0.3 OS:
Private report: No CVE-ID: None
 [2021-04-12 08:25 UTC] andre at webkr dot de
Description:
------------
When a function parameter has a non-nullable type hint, it is still possible to give it a default of null.

Test script:
---------------
function foo(string $foo = null) {
    var_dump($foo);
}
foo();

Expected result:
----------------
Fatal error: Cannot use null as default value for parameter $foo of type string

Actual result:
--------------
NULL

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-04-12 08:45 UTC] nikic@php.net
-Status: Open +Status: Not a bug
 [2021-04-12 08:45 UTC] nikic@php.net
For backwards-compatibility reasons, a null default value on a parameter type makes the type nullable.
 
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Mon May 16 16:05:46 2022 UTC