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
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: andre at webkr dot de
New email:
PHP Version: OS:

 

 [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

Pull Requests

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-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 13:01:31 2024 UTC