Re: PHP 7.1 - Address PHPSadness #28?
| From: | Robert Williams | Date: | Mon, 14 Sep 2015 20:17:19 +0000 |
| Subject: | Re: PHP 7.1 - Address PHPSadness #28? | ||
| References: | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-88190@lists.php.net to get a copy of this message | ||
I really don’t understand the resistance to this type of change, other than knowing that a fix
will necessarily be messy. The fact is, PHP distinguishes between a variable that has been declared
but defined to null and one that hasn’t been declared. The value of the first may safely be
assigned, compared, referenced, and so on, while attempting any of those things on the second one
results in an error message. The question of whether relying on those differences is philosophically
acceptable is a moot point: they *are* different, period, and the rest of the language should
acknowledge and support that — just as do most other languages with a similar design (e.g.,
JavaScript).
Beyond that, the philosophical debate is clearly settled in the real world of code. One glance
through the comments on the isset() documentation page, not to mention the bug reports and the
discussions on the lists, shows that people have long struggled with this. Based on code I’ve seen
from fairly experienced developers, the confusion around this has probably led to millions of
production bugs, many of them in the areas of validation and security. When a particular aspect of a
language's design promotes bugs to such an extent, it needs to be revisited.
I’m not sure what a good solution is, however. Changing isset() will have consequences: making it
return true for null will magically fix a lot of bugs out there, but it’ll also break code where
the programmer understood how it really works. Adding a parameter to control it is ugly. Extending
defined() to variables might be a good choice, as it’s already doing for constants exactly
what’s needed for variables; the only downside is that it would be better-named declared() than
defined(), but that’s the case for constants, too, so I can live with it. For now, isset() and
empty() can continue to work as-is but perhaps with a notice and deprecation when used with
undeclared variables.
--
Bob W.
Attachment: [application/pgp-signature] Message signed with OpenPGP using GPGMail signature.asc
Attachment: [application/pgp-signature] Message signed with OpenPGP using GPGMail signature.asc