Req #81177 [NEW]: Typehint parameter is ignored when passing NULL directly. Is it correct?

From: Date: Sat, 19 Jun 2021 18:49:44 +0000
Subject: Req #81177 [NEW]: Typehint parameter is ignored when passing NULL directly. Is it correct?
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-234504@lists.php.net to get a copy of this message
From: 6562680 at gmail dot com Operating system: Win10 PHP version: Irrelevant Package: Filter related Bug Type: Feature/Change Request Bug description:Typehint parameter is ignored when passing NULL directly. Is it correct? 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 bug report at https://bugs.php.net/bug.php?id=81177&edit=1 -- Fix committed: https://bugs.php.net/fix.php?id=81177&r=fixed Fixed in release: https://bugs.php.net/fix.php?id=81177&r=alreadyfixed Need backtrace: https://bugs.php.net/fix.php?id=81177&r=needtrace Need Reproduce Script: https://bugs.php.net/fix.php?id=81177&r=needscript Try newer version: https://bugs.php.net/fix.php?id=81177&r=oldversion Not developer issue: https://bugs.php.net/fix.php?id=81177&r=support Expected behavior: https://bugs.php.net/fix.php?id=81177&r=notwrong Not enough info: https://bugs.php.net/fix.php?id=81177&r=notenoughinfo Submitted twice: https://bugs.php.net/fix.php?id=81177&r=submittedtwice register_globals: https://bugs.php.net/fix.php?id=81177&r=globals PHP version support discontinued: https://bugs.php.net/fix.php?id=81177&r=phptooold Daylight Savings: https://bugs.php.net/fix.php?id=81177&r=dst IIS Stability: https://bugs.php.net/fix.php?id=81177&r=isapi Install GNU Sed: https://bugs.php.net/fix.php?id=81177&r=gnused Floating point limitations: https://bugs.php.net/fix.php?id=81177&r=float No Zend Extensions: https://bugs.php.net/fix.php?id=81177&r=nozend MySQL Configuration Error: https://bugs.php.net/fix.php?id=81177&r=mysqlcfg

« previous php.bugs (#234504) next »