laravel PHP 5.5.16 isset 或 empty 中的非法偏移类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25770012/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP 5.5.16 Illegal offset type in isset or empty
提问by Jeremy Harris
I'm working with Laravel here (semi-irrelevant) and am running into a weird PHP issues I've not seen before. I am receiving an exception with this error:
我在这里使用 Laravel(半不相关)并且遇到了我以前从未见过的奇怪的 PHP 问题。我收到此错误的异常:
Illegal offset type in isset or empty
isset 或 empty 中的非法偏移类型
The code is in the Laravel framework (Illuminate\View\Factory.php), and the relevant snippet throwing the error is:
代码在 Laravel 框架(Illuminate\View\Factory.php)中,抛出错误的相关代码段是:
if (isset($this->aliases[$view])) $view = $this->aliases[$view];
Now, I understand if you pass an array or object in as the array key, it will throw that error. But, I dumped out $this->aliases
and received:
现在,我明白如果您将数组或对象作为数组键传入,它会抛出该错误。但是,我放弃了$this->aliases
并收到了:
array(0) { }
And dumped out $view
and received:
并倾倒$view
并收到:
string(11) "layouts.app"
So, regardless of the fact that the array is empty, a call to isset
shouldsimply return false as the string key is not set.
因此,无论数组是否为空,调用都isset
应该简单地返回 false,因为未设置字符串键。
I don't believe this should be an error at all, but is there a setting in the php.ini that can cause such strict errors that I can change or am I just not understanding the fundamental operation of the isset()
method?
我不认为这应该是一个错误,但是 php.ini 中是否有一个设置会导致我可以更改的如此严格的错误,或者我只是不了解该isset()
方法的基本操作?
EDIT
编辑
This must be related to Mihai Stancu's comment below. I just tested this and it works fine without an exception:
这一定和下面米海斯坦库的评论有关。我刚刚对此进行了测试,它无一例外地正常工作:
$key = 'test-key';
$test = array();
if (isset($test[$key]))
var_dump('Yep');
else
var_dump('Nope');
That outputs "Nope" as expected.
这会按预期输出“Nope”。
回答by Krogertek
Simply declare $something = aliases[$view]
and parse it as a string and then
只需将其声明$something = aliases[$view]
并解析为字符串,然后
if (isset($this->$something))