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 Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
26 + 41 = ?
Subscribe to this entry?

 
 [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-2024 The PHP Group
All rights reserved.
Last updated: Sat Apr 27 01:01:30 2024 UTC