php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #81177 Typehint parameter is ignored when passing NULL directly. Is it correct?
Submitted: 2021-06-19 18:49 UTC Modified: 2021-06-19 21:30 UTC
From: 6562680 at gmail dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: Irrelevant OS: Win10
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: 6562680 at gmail dot com
New email:
PHP Version: OS:

Further comment on this bug is unnecessary.

 

 [2021-06-19 18:49 UTC] 6562680 at gmail dot com
Description:
------------
Usually it happens when another function pass its result to current, and it causes script to work another way than request.

1) If use just "$a" - it will fail is i didnt pass the argument
2) If use "int $a" - it will fail if argument is not an integer
3) If use "$a = 123" - if wont fail and if i DIDNT PASS argument - it will be 123, but i still can pass NULL and no fail there - seems correct, but logically NULL = undefined there.
4) If i use typehint "int $a = 123" - it will throw error if i pass NULL... What, i want 123 if i pass the null.
5) Ask my devs to ensure strict_types and rewrite all the project. Or just believe that "speakers" (they aren't coders i think) who just cry "use everywhere strict_types", and then solve that damn question marks in types...

For string its more complicated - empty string is string too...

Thank god we havent one more type = "undefined", in my 10 years practice that stuff was needed maybe... 2-3 times for functions that could return anything/mixed.

There's not only manual code, some variable could contain NULL too.

Currently i solve it like "section 2" in script textarea below. It allows "undefined" means - passed NULL or undefined, and controls integers. So on

Once i had just heared "You should use Classes instead of primitives all the time" and "strong-typed languages better than your php", i dont want to believe, but seems i should...

Test script:
---------------
// section 1

function hello($a = 123) {
    var_dump($a); // NULL
}

$a = null;
hello($a);


// section 2

function hello(int $a = null) {
  $a = $a ?? 123;
  var_dump($a); // 123
}


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-06-19 18:51 UTC] 6562680 at gmail dot com
There is one problem with "section 2" way - PHPStorm wont highlight the function has "default value"
 [2021-06-19 18:54 UTC] rtrtrtrtrt at dfdfdfdf dot dfd
> 1) If use just "$a" - it will fail is i didnt pass the argument

correct - it isn't an optional param

> 2) If use "int $a" - it will fail if argument is not an integer

correct

> 3) If use "$a = 123" - if wont fail and if i DIDNT PASS 
> argument - it will be 123, but i still can pass NULL

what else? it's an optional, NON-TYPED param

so WHAT is your problem?
 [2021-06-19 19:54 UTC] 6562680 at gmail dot com
My problem? No, seems it's like PHP problem, about we don't want the "undefined" type (i agree with)

Need some way in typehint that means "default value" if null passed or constant like infinity "UNDEFINED" that pass from parent function to child... not sure it will solve.

Currently we can do like "section 2" but when phpstorm autocompletes arguments - we wont see default values.

In my project its not a problem. When you prefer vendor libraries - its mandatory.

In short - typehint suggestions:
1. way to mark php null as undefined
2. way to detect empty string in type hint

Would like anybody finally suggests AGAIN class nesting and readonly properties and that request wont be ignored AGAIN.
But its another story...
 [2021-06-19 20:18 UTC] requinix@php.net
-Status: Open +Status: Not a bug -Type: Feature/Change Request +Type: Bug -Package: Filter related +Package: *General Issues
 [2021-06-19 20:18 UTC] requinix@php.net
https://www.php.net/manual/en/functions.arguments.php#functions.arguments.default

Every variable must have a value. Attempting to use a variable that does not have a value (because it hasn't been created yet) will "return" null, but that in no way means a null value represents undefined.

Specifying =null as the optional value for a parameter necessarily means that the parameter supports it, therefore calling the function with null as an explicit value is not prohibited.
 [2021-06-19 20:38 UTC] 6562680 at gmail dot com
Last 5 years you solving problems with a words.

Even then once i say "would be great to use starts()" function and after 2 years FINALLY you decide to create it... and even then it returns false creates one more if.

Speaking speaking speaking explaining explaining...

In that time we've got PHP8 with union types without array types, still havent generics, still havent nested classes and readonly properties, havent internal dependency injector, havent that damn decorators...

But well, we have something "CurlHandle" that interfaced resource handler, some ArrayPrototypiers and you have discussion to multiline call syntax
"
attr
> call
> call
> call
"

that expected only in SCRIPTS deployed on microservices without OOP and patterns...

Continue speaking. Time is on other side
 [2021-06-19 20:55 UTC] a at b dot c
Pull requests are welcome.
 [2021-06-19 21:14 UTC] rtrtrtrtrt at dfdfdfdf dot dfd
> My problem? No, seems it's like PHP problem

no

> about we don't want the "undefined" type

besides that NULL is not undefined just use a typehint and strict-types
PEBCAK
 [2021-06-19 21:30 UTC] nikic@php.net
-Block user comment: No +Block user comment: Yes
 [2021-06-19 21:30 UTC] nikic@php.net
This does not seem like a particularly productive discussion...
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 07:01:28 2024 UTC