简体   繁体   English

PHP 7 致命错误:未捕获错误:无法使用字符串偏移量作为数组

[英]PHP 7 Fatal error: Uncaught Error: Cannot use string offset as an array

Before anyone mark my question as duplicate, I wish to clarify that my case is unique and specific to PHP 7 (I use PHP 7.1.1 in UniformServer).在任何人将我的问题标记为重复之前,我想澄清一下,我的案例是独一无二的并且特定于 PHP 7(我在 UniformServer 中使用 PHP 7.1.1)。 I have seen all other similar questions here and they did not work for me.我在这里看到了所有其他类似的问题,但它们对我不起作用。

I had a working code as below:我有一个工作代码如下:

In class constructor:在类构造函数中:

$this->fldSection = "header";

down below in another function:在下面的另一个函数中:

$_ENV['validate'][$this->fldSection][$this->name] = $params;

This is working fine in PHP 5.6.这在 PHP 5.6 中运行良好。 However this gives the infamous "Cannot use string offset as an array.." error in PHP 7.1.1 for $this->fldSection = 'header' when it is set in the $_ENV variable然而,这会在 PHP 7.1.1 中为$this->fldSection = 'header'$_ENV变量中设置时出现臭名昭著的“无法使用字符串偏移量作为数组..”错误

All my variables are dynamically filled.我所有的变量都是动态填充的。 Is there a different/better way to accomplish this in PHP 7.1.1?在 PHP 7.1.1 中是否有不同/更好的方法来实现这一点?

I finally cracked the reason for this issue.我终于破解了这个问题的原因。 I'm using the super-global $_ENV to store my values and trying to assign variable-variables to it.我正在使用超级全局$_ENV来存储我的值并尝试为其分配变量变量。 However from PHP documentation I find that variable-variables are not supported on super-globals.但是,从 PHP 文档中我发现超全局变量支持变量。 I changed the variable to $GLOBAL instead of $_ENV (like below) and it worked.我将变量更改为$GLOBAL而不是$_ENV (如下所示)并且它起作用了。

$GLOBAL['validate'][$this->fldSection][$this->name] = $params;

I'm not sure why it worked on 5.6 and not on 7.1.我不确定为什么它适用于 5.6 而不是 7.1。 I guess PHP has probably started enforcing those rules strictly only from v7.我猜 PHP 可能只从 v7 开始严格执行这些规则。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM