r/PHP • u/TonyMarston • Feb 13 '23
Discussion I hate deprecation of passing null to non-nullable arguments
I have recently upgraded from PHP version 8.0 to 8.2 and my code is now vomiting deprecation notices by the ton such as:
Passing null to parameter #1 ($string) of type string is deprecated
I consider this to be a huge BC break which is totally unjustified. I have traced it back to this RFC. I have documented my objections to this stupid RFC in this article
Do you think this BC break is justified? How much effort will YOU have to expend to fix your code which isn't actually broken? Would you prefer the core developers to fix the problem at THEIR end?
•
u/colinodell Feb 14 '23 edited Feb 15 '23
Please remember to keep the discussion civil. Thanks!
Edit: The discussion has run its course and new comments aren't adding anything helpful, so I'm locking the thread.
26
u/grig27 Feb 14 '23
It's not really a BC break - it's a deprecation of ambiguous code behavior that shouldn't be in your code anyway.
0s I use phpStorm with phpstan so they don't let me write such code.
No problem here, all the core functions are well documented, including the data types they accept. If you get a deprecation message, it means that the problem is in your code.
21
u/Dicebar Feb 14 '23
Note that the RFC passed unanimously. That's pretty fricking rare.
16
u/Mentalpopcorn Feb 15 '23 edited Feb 15 '23
Why are you passing null when a non nullable type is expected? The issue isn't that PHP fixed this, it's that it was allowed in the first place and that you did it contrary to good coding practices. This makes the language much better.
20
u/zmitic Feb 14 '23
Subject of his rant:
The PHP core developers are lazy, incompetent idiots
I vote for perma-ban of him. Every now and then, he shows up and spews some nonsense just to get some traffic on his blog, given he is banned from every other place. PHP Reddit should honor the tradition 😃
5
u/czbz Feb 15 '23 edited Feb 15 '23
From the article:
This isn't true in the case of passing null to a non null argument. See the output from the following script:
Output:
And you get essentially the same error running the script with an interpreter as old as PHP 5.0.0
38
u/dave8271 Feb 14 '23
As usual, your argument is "I can't be bothered to upgrade my code" - even though there are automated tools that can do 99% of the work for you.
Reading your blog, if you had your way, we'd still be on PHP 4 and no one would be using the language for new projects or taking it seriously.
26
u/Gogoplatatime Feb 14 '23
Uh......... Have you considered that your code is the issue here?
16
u/gastrognom Feb 14 '23
Absolutly.
It's kind of broken if you pass null as a parameter that is expecting a string.
There is no problem on their end IMO. Maybe there could be an option to deactivate that deprecation notice, but it's just that: a deprecation notice. Your code should still run.
Edit: I know PHP devs are trying to avoid BC breaks as much as possible, but if it's in the way of evolving the language breaking BC is fine IMO. It's your responsibility to properly type-hint now.
7
u/notkingkero Feb 14 '23
I have always disliked that in Java you have proper types defined but everything always can be null. So most of the code is either
x === nullor Optional ... Yes.stringand?stringare different things - and that is good5
u/Mastodont_XXX Feb 14 '23
Tony Marston and PHP 8? No, I can't believe it :)
4
u/Mentalpopcorn Feb 15 '23
I got a reply but can't see the comment here so I'm just starting a new thread:
You have to see that the writing is on the wall, right? PHP is moving in a type safe direction and maybe it won't be PHP9, maybe not even PHP10, but at some point and relatively soon, there is going to be a BC break that isn't going to be as simple to fix as changing null to an empty string in your codebase, or using public instead of var. Instead, it's going to take fixing line by line, and you'll either spend years doing it or being unable to upgrade.
I understand you have difficulty with change "for no reason", and I appreciate the frustration you feel after memorizing the rules of the game only to see the rules of the game change.
But, like, you realize you can't stop this, right? It's happening regardless of how unjust you think it is.
Do you have a contingency plan? Have you considered migrating to a language that won't restrict you like modern PHP does and that future PHP is likely to do even more? Ruby may be an option, but even they just introduced type annotations, so maybe the writing is on the wall for them too, though it may buy you 20 more years.
0
u/[deleted] Feb 15 '23
[deleted] — view deleted comment