PHP7 method_exists未捕获的错误:函数名称必须是字符串 [英] PHP7 method_exists Uncaught Error: Function name must be a string

查看:150
本文介绍了PHP7 method_exists未捕获的错误:函数名称必须是字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到此错误:

致命错误:未被捕获的错误:函数名称必须为字符串

Fatal error: Uncaught Error: Function name must be a string in

对于此代码:

if (function_exists($item['function'])) {
    $item['function']($item, $default);
} elseif (method_exists($this, $item['function'])) {
    $this->$item['function']($item, $default);
}

我知道将代码更改为

if (function_exists($item['function'])) {
    $item['function']($item, $default);
} elseif (method_exists($this,$item['function'])) {
    $this->{$item['function']}($item, $default);
}

解决了该错误,但我的问题是,此行是否应该

Solved that error, but my question is, should this line

 $item['function']($item, $default);

也将转换为

{$item['function']}($item, $default);

还是可以原样保留?

推荐答案

这是由于不兼容的更改,按评估顺序处理间接变量和方法:

This is due to incompatible changes in the order-of-evaluation for processing indirect variables and methods:

更改对间接变量,属性和方法的处理

现在将严格按照从左到右的顺序评估对变量,属性和方法的间接访问,这与以前的特殊情况相反.下表显示了评估顺序的变化.

Indirect access to variables, properties, and methods will now be evaluated strictly in left-to-right order, as opposed to the previous mix of special cases. The table below shows how the order of evaluaiton has changed.

不,您不必更改此行:

$item['function']($item, $default);

因为这里没有进行特殊的评估,所以它将仅使用array元素作为函数名称并调用该函数.您可以对其进行更改,并且代码仍将正常运行,但这不是必需的.

Because there is no special evaluation going on here, it will just use the array element as function name and call the function. You could change it, and the code will still work properly, but it isn't necessary.

但是您已经正确地进行了操作,因此必须进行以下更改:

But as you already did correctly you have to change:

$this->$item['function']($item, $default);

收件人:

$this->{$item['function']}($item, $default);
       ↑                 ↑

自从您在此中可以看到:

Since as you can see in this table:

                    Old and new evaluation of indirect expressions
      Expression            PHP 5 interpretation         PHP 7 interpretation
-------------------------------------------------------------------------------
  $$foo['bar']['baz'] |     ${$foo['bar']['baz']}  |    ($$foo)['bar']['baz']
  $foo->$bar['baz']   |     $foo->{$bar['baz']}    |    ($foo->$bar)['baz']
  $foo->$bar['baz']() |     $foo->{$bar['baz']}()  |    ($foo->$bar)['baz']()
  Foo::$bar['baz']()  |     Foo::{$bar['baz']}()   |    (Foo::$bar)['baz']()

PHP 7将假定您首先要访问一个对象属性,然后要从该属性访问索引,并使用其值作为方法名称来调用方法(从左到右的顺序).

PHP 7 will assume you first want to access an object property, and then you want to access an index from that property, and use its value as method name to call a method (left-to-right order).

要将变量和索引用作属性名称,必须使用花括号来表示.

To use the variable and index as property name, you have to use curly braces to indicate that.

这篇关于PHP7 method_exists未捕获的错误:函数名称必须是字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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