On Thu, Jan 27, 2022, at 2:24 AM, Christian Schneider wrote:
> Am 27.01.2022 um 05:17 schrieb Mark Randall <marand...@php.net>:
>> It is my firm believe that a language such as PHP should not be dependent on 
>> static analysers to protect users against engine-level problems that may 
>> occur during runtime.
>
> My issue with this is that while it seems to work towards the goal of 
> "fail fast" it does not completely fulfill its promise.
> 
> What do you gain by making undefined variables an error? You force 
> developers to sprinkle their code with variable initializations where 
> they might be sure that the well-defined behavior of undefined 
> variables is fine in their code. Why is that a problem?
>
> One of the burdens of early statically typed languages was that you had 
> to declare everything in detail. It was with the advent of type 
> inference that some of the typing overhead could be reduced and a 
> balance between safety and readability through DRY was reached.
>
> Forcing to initialize everything moves PHP against DRY while the safety 
> gained by aborting the program is debatable, even though better 
> approaches (like static analysis) are available now. To me it's about 
> using the right tool for the right job.

That ship sailed in 8.0 when undefineds were promoted to Warnings.  Undefined 
variables are already a "you need to fix this, the engine says you should feel 
bad" issue.  For better or worse.

I don't think it's as dire as you claim, though.  We're not talking about PHP 
going the way of old-C, where you had to pre-define all variables at the start 
of the function.  You can still define variables on-the-fly when you first 
*write* to them.  It's when you first *read* from them that is a very bad time 
to be stealth-defining a variable.  Anything correct that happens at that point 
is by chance, not by design.  It's already a presumptive bug everywhere it 
happens, modulo a few edge cases (like the counter array someone else brought 
up, which is valid).

That said, I am not convinced that a fatal is the correct answer, either.  In 
an interpreted language, we can't guarantee correctness until executing the 
code.  IDEs and other static tools already complain about undefineds.  The 
engine already yells in your face about it if you ignore the analyzers.  I am 
not sure what additional value is gained by making the scream even louder.

--Larry Garfield

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: https://www.php.net/unsub.php

Reply via email to