PHP - Why do different versions of PHP return different results when I use $this as a dynamic variable in isset()?
Asked Answered
C

1

6

In PHP 7.0:

$a = 'this';
return isset( $$a );
// returns true

But in PHP 7.1:

$a = 'this';
return isset( $$a );
// returns false

Does anyone know why this happens?

Confinement answered 25/6, 2018 at 11:31 Comment(6)
Really? 3v4l.org/ARNhTDonella
@Donella yes, and the PHP version I used was 7.0.8Confinement
But you see this? Output for 5.6.30, hhvm-3.18.5 - 3.22.0, 7.0.30 - 7.3.0alpha1?Donella
@Andreas, the question probably means something like 3v4l.org/kQ271.Gerius
And for comparison: 3v4l.org/RDJa0Snubnosed
@Gerius yes, that's what I meantConfinement
P
7

This is related to this change in 7.1:

Inconsistency fixes to $this

Whilst $this is considered a special variable in PHP, it lacked proper checks to ensure it wasn't used as a variable name or reassigned. This has now been rectified to ensure that $this cannot be a user-defined variable, reassigned to a different value, or be globalised.

http://php.net/manual/en/migration71.other-changes.php#migration71.other-changes.inconsistency-fixes-to-this

This RFC explains it in more detail, though it also says:

Disable ability to re-assign $this indirectly through $$

An attempt to re-assign $this through $$ assignment will lead to throwing of Error exception.

$a = "this";
$$a = 42; // throw new Error("Cannot re-assign $this")

It's still possible to read $this value through $$.

(Emphasis mine.)

isset seems to have its own special treatment of $$ for $this which prohibits it from seeing it. I'm not sure if that's intentional or a byproduct of these changes.

Personalty answered 25/6, 2018 at 11:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.