Re: PHP 7.1 - Address PHPSadness #28?
| From: | Rowan Collins | Date: | Wed, 16 Sep 2015 21:09:09 +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-88261@lists.php.net to get a copy of this message | ||
On 16 September 2015 20:56:22 BST, Robert Williams <rewilliams@thesba.com> wrote:
>On Sep 16, 2015, at 11:14, Rowan Collins <rowan.collins@gmail.com>
>wrote:
>>
>> Robert Williams wrote on 16/09/2015 18:37:
>>> If they were null, then PHP wouldn’t spit out errors about undefined
>variable accesses
>>
>> Once again - PHP does not spit out errors when you access an
>undefined variable. It spits out Notices in case you've done it by
>mistake. Maybe you're thinking of JavaScript?
>
>In our shop, notices are most definitely errors. And notices aren’t
>graceful error handling. I think PHP also also regards them as errors
>(thus the ability to catch them with an error handler) - it’s just a
>lower grade of error.
I think the term "error" in "error handler" is just a misleading historical
accident, like "type hint" (which does produce an error!). I can certainly sympathise with
wanting to run without any notices - they are generally hints for writing better code. In the vast
majority of cases, though, that means working out *why* the variable is undefined, otherwise you
might as well just use the @ operator to say "I don't care about this notice".
>That’s great in theory, but in practice, programmers’ code must
>frequently collaborate with other people’s code, often code from
>outside their organization. In these cases, they don’t have the control
>to just fix the problem on the other side.
This still implies that there's a common problem to fix, which I just don't believe. The
number of places where the following all hold true must be vanishingly small:
- the third-party code is dumping bare variables into your scope (globals, or a file-level include
are the only mechanisms I can think of)
- they do so unreliably, in the sense that different variables will be set under different
circumstances
- you/they have used null as a value which needs to be positively asserted, not taken as the default
state, so that isset() does not meet your needs
- you have no way of presetting the variable to some other terminal value to detect when it is
explicitly set
> And in some cases, like with
>config files that set bare variables, there’s really nothing wrong with
>the way it’s done that needs to be fixed. Some folks might prefer to
>use a different model, but that’s just a preference.
Well, there's still a fix beyond "ignore the notices by using exists()" - you could
preset all the variables to their default values, or a sentinel "illegal value", before
including the config file.
Still, at least it's a concrete example at last.
>In a dynamic
>language like PHP, I really don’t understand how folks can *not* see
>that as an oversight.
I've not found evidence that any other language treats variables in the proposed way, so it
really doesn't feel like an "oversight" to me, but a request for a new and quite
unusual feature.
> Further, we can do this for everything else…
>constants, array elements, object properties, functions, and even
>classes, so why not variables?
A fair question; one of the big differences with things like classes and functions is that
they're declared, once, and then fixed; their scope is also likely to cross module boundaries -
a class definition is accessible from any scope, for instance. A local variable has none of these
properties - on the one hand it has no explicit declaration, only "initialisation" the
first time you write to it; and on the other, it is a label that exists only inside the current
function.
But yes, from a completeness point of view, it does make some sense. My fear is that the people who
don't understand why isset() does what it does will think it's the answer to some
imaginary problem, and end up more confused than ever when their code breaks. If the function had
been proposed as a debugging aid, maybe filed under Reflection or debug_variable_is_initialised, I
might have more sympathy, but that's not how it's been presented.
> There's even have precedence for
>controlling behavior based on variable existence in extract(), so PHP
>itself does it in ways that directly affect the user, but the user
>can’t do it.
I can see where you're coming from, but other than not having such a function pair at all
(which I'd personally be fine with - they feel like holdovers from a bygone age) I'm not
sure how else compact()/extract() could work. It's not so much that they check variable
existence, as that the list of uninitialised variables is infinite, so would be tricky to work
with...
Regards,
--
Rowan Collins
[IMSoP]