Req->Bug #81177 [Opn->Nab]: Typehint parameter is ignored when passing NULL directly. Is it correct?

From: Date: Sat, 19 Jun 2021 20:18:53 +0000
Subject: Req->Bug #81177 [Opn->Nab]: Typehint parameter is ignored when passing NULL directly. Is it correct?
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-234511@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=81177&edit=1 ID: 81177 Updated by: requinix@php.net Reported by: 6562680 at gmail dot com Summary: Typehint parameter is ignored when passing NULL directly. Is it correct? -Status: Open +Status: Not a bug -Type: Feature/Change Request +Type: Bug -Package: Filter related +Package: *General Issues Operating System: Win10 PHP Version: Irrelevant Block user comment: N Private report: N New Comment: 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. Previous Comments: ------------------------------------------------------------------------ [2021-06-19 19:54:56] 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 18:54:36] 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 18:51:55] 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:49:43] 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 } ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=81177&edit=1

« previous php.bugs (#234511) next »