# [RFC] Undefined Variable Error Promotion

- Thread: https://externals.io/message/117118
- Original archive: https://news-web.php.net/php.internals/117118
- Started by Marco Pivetta on 2022-02-22T13:56:44+00:00
- Messages: 15
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #117063 — Rowan Tommins — 2022-02-18T11:23:58+00:00

URL: https://externals.io/message/117118#117063

> I present:
>
> https://wiki.php.net/rfc/undefined_variable_error_promotion


It would be good to have a "Scope" or "Unaffected Functionality" section 
here, because there are a number of closely related things which were 
also raised from Notice to Warning in 8.0:

- undefined array keys
- undefined object properties
- array access on a non-array
- property access on a non-object

I think it is sensible to discuss those separately, but it would be good 
to make that clear.


Similarly, it would be good to have more discussion of what "accessing" 
means, as the current examples are quite narrow, only showing direct use 
and the ++ operator. Other functionality potentially affected:

- passing the variable to a function, presumably excluding by-reference 
parameters which don't currently warn
- all the combined assignment operators - 
https://www.php.net/manual/en/language.operators.assignment.php
- the array append operator ($a[] = 42;) does NOT currently give an 
undefined variable Warning
- variable variables, e.g. "$b = 'a'; echo $$b;"
- the `compact()` pseudo-function

There's probably others that I've missed.


Regards,

-- 
Rowan Tommins
[IMSoP]

---

## #117112 — Nicolas Grekas — 2022-02-22T09:15:07+00:00

URL: https://externals.io/message/117118#117112 · In reply to: #117063

Le ven. 18 févr. 2022 à 12:24, Rowan Tommins <rowan.collins@gmail.com> a
écrit :

> 
> > […]
>
>
> It would be good to have a "Scope" or "Unaffected Functionality" section
> here, because there are a number of closely related things which were
> also raised from Notice to Warning in 8.0:
>
> - undefined array keys
> - undefined object properties
> - array access on a non-array
> - property access on a non-object
>
> I think it is sensible to discuss those separately, but it would be good
> to make that clear.
>
>
> Similarly, it would be good to have more discussion of what "accessing"
> means, as the current examples are quite narrow, only showing direct use
> and the ++ operator. Other functionality potentially affected:
>
> - passing the variable to a function, presumably excluding by-reference
> parameters which don't currently warn
> - all the combined assignment operators -
> https://www.php.net/manual/en/language.operators.assignment.php
> - the array append operator ($a[] = 42;) does NOT currently give an
> undefined variable Warning
> - variable variables, e.g. "$b = 'a'; echo $$b;"
> - the `compact()` pseudo-function
>
> There's probably others that I've missed.
>

I 100% agree with that. An "Unaffected Functionality" section would be much
welcome.

I would add to the list: isset($foo) when $foo is first seen.
(To me this should not throw anything, like for uninitialized properties.)

If the RFC is about promoting the current warnings to Error without adding
any other Error at places that currently don't throw any warnings, then it
could be useful to say so. And if the RFC is going to propose to throw
Error at places that are currently warning-less, the list should be
explicit.

I very much call for an implementation to be provided before starting any
vote on the topic btw.
This is typically the kind of topic where getting into the actual code
might help spot edge cases.

Thanks for the RFC btw!

Nicolas

---

## #117113 — Mark Randall — 2022-02-22T11:07:46+00:00

URL: https://externals.io/message/117118#117113 · In reply to: #117112

> I very much call for an implementation to be provided before starting any
> vote on the topic btw.
The RFC states on its first line that accessing an undefined variable 
emits an E_WARNING. If it does not currently emit an `E_WARNING` then it 
is out of scope of this RFC.

It is not sensible to provide a full implementation for something that 
will require extensive engine and test changes for something several 
years into the future, during which many other changes will be made.

A sensible time for this would be after the branch is cut for 8.4.

---

## #117114 — Mark Randall — 2022-02-22T11:32:48+00:00

URL: https://externals.io/message/117118#117114 · In reply to: #117063

> - undefined array keys
> - undefined object properties
> - array access on a non-array
> - property access on a non-object


I'd encourage those to be discussed, but they are unrelated to this RFC.

I think most of them would pass fairly easily, but undefined array keys 
is out the outlier and has a reasonable chance of being rejected at this 
time. However, with their being 4 years before the launch of 9.0 (and 
the next opportunity to change it) opinion may change between now and then.

I've updated the RFC to define accessing a variable as attempting to 
read its value for use in an expression without accounting for the 
possibility of it being unset.

I have also included the warning message to narrow this down further, 
and noted that things which account for undefined variables such as 
isset, empty and null coalesce will remain unaffected.

If there's am existing formal definition someone wishes to contribute, 
I'd be happy to update it.

One thing maybe worth mentioning is that while most operators such as 
++, +=, .= etc will be covered by this change, $undefined[] = 1; will 
not be.

Mark Randall

---

## #117116 — Nick Wallace — 2022-02-22T12:25:54+00:00

URL: https://externals.io/message/117118#117116 · In reply to: #117112

I have tried to no avail to be removed from this list. So I will just spam
this until I am removed.

On Tue, Feb 22, 2022, 3:15 AM Nicolas Grekas <nicolas.grekas+php@gmail.com>
wrote:

> Le ven. 18 févr. 2022 à 12:24, Rowan Tommins <rowan.collins@gmail.com> a
> écrit :
>
> > […]
>
> I 100% agree with that. An "Unaffected Functionality" section would be much
> welcome.
>
> I would add to the list: isset($foo) when $foo is first seen.
> (To me this should not throw anything, like for uninitialized properties.)
>
> If the RFC is about promoting the current warnings to Error without adding
> any other Error at places that currently don't throw any warnings, then it
> could be useful to say so. And if the RFC is going to propose to throw
> Error at places that are currently warning-less, the list should be
> explicit.
>
> I very much call for an implementation to be provided before starting any
> vote on the topic btw.
> This is typically the kind of topic where getting into the actual code
> might help spot edge cases.
>
> Thanks for the RFC btw!
>
> Nicolas

---

## #117117 — Nicolas Grekas — 2022-02-22T13:53:05+00:00

URL: https://externals.io/message/117118#117117

Le mar. 22 févr. 2022 à 12:07, Mark Randall <marandall@php.net> a écrit :

> 
> > […]
> The RFC states on its first line that accessing an undefined variable
> emits an E_WARNING. If it does not currently emit an `E_WARNING` then it
> is out of scope of this RFC.
>

This last sentence could be worth copy/pasting into the RFC :)
Thanks for the recent edits, they help clarify to me.


> It is not sensible to provide a full implementation for something that
> will require extensive engine and test changes for something several
> years into the future, during which many other changes will be made.
>

> A sensible time for this would be after the branch is cut for 8.4.
>

I understand if the target is to do everything in 9.0.
But this makes me think: we should trigger a deprecation just before all
corresponding warnings!
This would signal the change to 8.x users and help them spot where a change
is needed.
This should happen before the warning to that error handlers that turn the
warning into an exception also get a note about the soon-to-be change.

WDYT?

Nicolas

---

## #117118 — Marco Pivetta — 2022-02-22T13:56:44+00:00

URL: https://externals.io/message/117118#117118 · In reply to: #117117

On Tue, Feb 22, 2022 at 2:53 PM Nicolas Grekas <nicolas.grekas+php@gmail.com>
wrote:

>
> But this makes me think: we should trigger a deprecation just before all
> corresponding warnings!
>

Please, no more deprecation warnings, make it stop 😥
Yes, undefined variables are a problem, but I just spent 6 months in
`vendor/` code for 8.0->8.1 stuff, and it doesn't bring anything but
frustration: this stuff is statically introspectible, and even more
side-effects are just more trouble.

Marco Pivetta

http://twitter.com/Ocramius

http://ocramius.github.io/

---

## #117123 — Nicolas Grekas — 2022-02-23T15:29:55+00:00

URL: https://externals.io/message/117118#117123 · In reply to: #117118

Le mar. 22 févr. 2022 à 14:56, Marco Pivetta <ocramius@gmail.com> a écrit :

> On Tue, Feb 22, 2022 at 2:53 PM Nicolas Grekas <
> nicolas.grekas+php@gmail.com> wrote:
>
> > […]
>
> Please, no more deprecation warnings, make it stop 😥
> Yes, undefined variables are a problem, but I just spent 6 months in
> `vendor/` code for 8.0->8.1 stuff, and it doesn't bring anything but
> frustration: this stuff is statically introspectible, and even more
> side-effects are just more trouble.
>

I'm not going to be affected by this RFC at all, and neither are you, since
we use throwing error handlers. But ppl that do rely on code bases that
have undefined vars "by design" will be. I would bet that the number of ppl
in that affected group and that also use a static analyser is very very
small. This means that static analysers are not a pragmatic solution here.

Ppl that don't use static analysers deserve a prior notice. There is a
dedicated reporting mechanism in place and we should use it IMHO. With new
deprecations added to PHP 8.1, the ecosystem realized that the tooling
needed to improve - and it did (phpunit, Laravel, etc.). We can and should
add new runtime deprecations when planning a BC break.

Please consider adding this deprecation Mark (and others.)

Nicolas

---

## #117124 — Christoph M. Becker — 2022-02-23T16:59:40+00:00

URL: https://externals.io/message/117118#117124 · In reply to: #117123

> Le mar. 22 févr. 2022 à 14:56, Marco Pivetta <ocramius@gmail.com> a écrit :
>
> > […]
>
> I'm not going to be affected by this RFC at all, and neither are you, since
> we use throwing error handlers. But ppl that do rely on code bases that
> have undefined vars "by design" will be. I would bet that the number of ppl
> in that affected group and that also use a static analyser is very very
> small. This means that static analysers are not a pragmatic solution here.

That.

> Ppl that don't use static analysers deserve a prior notice. There is a
> dedicated reporting mechanism in place and we should use it IMHO. With new
> deprecations added to PHP 8.1, the ecosystem realized that the tooling
> needed to improve - and it did (phpunit, Laravel, etc.). We can and should
> add new runtime deprecations when planning a BC break.
>
> Please consider adding this deprecation Mark (and others.)

Do you mean `E_DEPRECATED` in addition to E_WARNING, or instead?  I
wouldn't be happy either way.

--
Christoph M. Becker

---

## #117125 — Nicolas Grekas — 2022-02-23T18:02:09+00:00

URL: https://externals.io/message/117118#117125 · In reply to: #117124

Le mer. 23 févr. 2022 à 17:59, Christoph M. Becker <cmbecker69@gmx.de> a
écrit :

> 
>
> > […]
> écrit :
> > […]
> all
> > […]
> since
> > […]
> ppl
> > […]
> here.
>
> That.
>
> > […]
> new
> > […]
> should
> > […]
>
> Do you mean `E_DEPRECATED` in addition to E_WARNING, or instead?  I
> wouldn't be happy either way.
>

I mean in addition yes, deprecation before warning.

I considered grouping them as E_DEPRECATED|E_WARNING but that'd break many
userland error handlers I fear.

---

## #117126 — Marco Pivetta — 2022-02-23T18:14:26+00:00

URL: https://externals.io/message/117118#117126 · In reply to: #117125

Yeah, please don't: yet another side effect, deep in some `vendor` cruft.

Just stop it, please: I almost have PTSD from 8.1.

On Wed, 23 Feb 2022, 19:02 Nicolas Grekas, <nicolas.grekas+php@gmail.com>
wrote:

> Le mer. 23 févr. 2022 à 17:59, Christoph M. Becker <cmbecker69@gmx.de> a
> écrit :
>
> > […]
>
> I mean in addition yes, deprecation before warning.
>
> I considered grouping them as E_DEPRECATED|E_WARNING but that'd break many
> userland error handlers I fear.

---

## #117127 — Mark Randall — 2022-02-23T18:42:15+00:00

URL: https://externals.io/message/117118#117127 · In reply to: #117125

> I mean in addition yes, deprecation before warning.

I don't see this happening.

An engine warning is as stark a mechanism as we have available that you 
either made a mistake, or shouldn't be doing something, without 
preventing you from actually doing it.

It may be that in 8.2 if this RFC passes that message will change to 
include "This will throw an error in PHP 9", but giving multiple 
messages for it is not beneficial IMO.

Mark Randall

---

## #117128 — Rowan Tommins — 2022-02-23T19:32:10+00:00

URL: https://externals.io/message/117118#117128

> It may be that in 8.2 if this RFC passes that message will change to 
> include "This will throw an error in PHP 9"


I think that wording change should be part of the proposed change in the 
RFC. Otherwise, a lot of people simply won't know the decision to remove 
it has been made and will be surprised when 9.0 comes around.

I used a similar approach in 
https://wiki.php.net/rfc/deprecate-bareword-strings because I wanted the 
visibility of `E_WARNING` but the clear mention of deprecation.

Regards,

-- 
Rowan Tommins
[IMSoP]

---

## #117129 — Mark Randall — 2022-02-23T23:03:47+00:00

URL: https://externals.io/message/117118#117129 · In reply to: #117128

On 23/02/2022 19:32, Rowan Tommins wrote
> I think that wording change should be part of the proposed change in the 
> RFC. Otherwise, a lot of people simply won't know the decision to remove 
> it has been made and will be surprised when 9.0 comes around.

It is already part of the RFC within the "proposed PHP versions" section.

---

## #117133 — Rowan Tommins — 2022-02-24T08:59:16+00:00

URL: https://externals.io/message/117118#117133

> On 23/02/2022 19:32, Rowan Tommins wrote
> > […]
>
> It is already part of the RFC within the "proposed PHP versions" section.
>

It doesn't mention any particular wording, and says the change "may be 
included". I'm not sure what that means. If I vote "yes", am I writing a 
blank cheque for anyone to change the message to anything? More 
importantly, am I approving removal even if the warning message isn't 
changed?

I'm not sure when else this decision would be made, other than as part 
of the RFC, so am suggesting the "may" be changed to "will", and a 
specific wording proposed. Or, if you think changing the message isn't 
necessary, "may" can be changed to "will not", and people can vote on 
that basis.

Regards,

-- 
Rowan Tommins
[IMSoP]
