PHP - 当我在 isset() 中使用 $this 作为动态变量时,为什么不同版本的 PHP return 会产生不同的结果?
PHP - Why do different versions of PHP return different results when I use $this as a dynamic variable in isset()?
在 PHP 7.0 中:
$a = 'this';
return isset( $$a );
// returns true
但在 PHP 7.1 中:
$a = 'this';
return isset( $$a );
// returns false
有人知道为什么会这样吗?
这与 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.
This RFC 更详细地解释了它,尽管它还说:
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 $$
.
(强调我的。)
isset
似乎对 $$
对于 $this
有自己的特殊处理,禁止它看到它。我不确定这是有意为之还是这些更改的副产品。
在 PHP 7.0 中:
$a = 'this';
return isset( $$a );
// returns true
但在 PHP 7.1 中:
$a = 'this';
return isset( $$a );
// returns false
有人知道为什么会这样吗?
这与 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.
This RFC 更详细地解释了它,尽管它还说:
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$$
.
(强调我的。)
isset
似乎对 $$
对于 $this
有自己的特殊处理,禁止它看到它。我不确定这是有意为之还是这些更改的副产品。