简体   繁体   中英

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). 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. 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

All my variables are dynamically filled. Is there a different/better way to accomplish this in 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. However from PHP documentation I find that variable-variables are not supported on super-globals. I changed the variable to $GLOBAL instead of $_ENV (like below) and it worked.

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

I'm not sure why it worked on 5.6 and not on 7.1. I guess PHP has probably started enforcing those rules strictly only from v7.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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