不能使用 [] 进行阅读 [英] Cannot use [] for reading

查看:44
本文介绍了不能使用 [] 进行阅读的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的一个脚本中,我尝试执行以下操作

In one of my scripts, I try to do the following

$data[] = self::get($row['sr_id']); // <-- line 55

但是,PHP 不允许我这样做,给了我这个错误

However, PHP does not allow me to do this, giving me this error

致命错误:无法使用 [] 读取第 55 行的/file.php

Fatal error: Cannot use [] for reading in /file.php on line 55

self::get 函数返回一个布尔值或一个对象.

The self::get function return either a bool, or an object.

get 函数创建一个新对象,该对象再次从 mysql 数据库加载数据.

The get function creates a new object which again loads data from a mysql database.

推荐答案

旧的 PHP 版本在表达式中接受 $var[],允许读取 $var 内容,不管的语法.PHP 5.1 将其设为非法.但有时错误是在预期上下文之外触发的.
所以我的猜测 (再次:显示更多代码) 是前一行包含一个未完成的表达式,$data[] 加入了该表达式.

Old PHP versions accepted $var[] in expressions, allowed reading out the $var content regardless of syntax. PHP 5.1 made that illegal. But sometimes the error is triggered outside of the intented context.
So my guess (again: show more code) is that the preceeding line contains an unfinished expression, to which the $data[] joins.

在对象属性的情况下,您可以将 $data var 包装到 { } 中,但这似乎不是您的情况的问题.(否则,第 54 行中有一些您没有显示的内容.)右侧无法合理触发错误.即使数组访问 [] 一个整数或对象也不会触发那个致命错误.

In case of object attribute you can wrap your $data var into { }, but that doesn't seem to be the problem in your case. (Else there is something in line 54, that you didn't show.) The right hand side can't reasonably trigger the error. Even array accessing [] an integer or object wouldn't trigger that fatal error.

所以如果没有任何帮助,只需使用 array_push().解决 PHP.

So if nothing helps, just use array_push(). Work around PHP.

这篇关于不能使用 [] 进行阅读的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆