gpt4 book ai didi

数组值上的 PHP 全局关键字

转载 作者:可可西里 更新时间:2023-10-31 22:50:22 25 4
gpt4 key购买 nike

嘿嘿

我目前正在处理其他人为 WordPress 编写的损坏主题。全新安装后会抛出 500 错误。在我的本地机器上检查代码后,我可以检查以下内容:

    public static function skip_script($conf) {
$hook_suffix = isset($GLOBALS['hook_suffix']) ? $GLOBALS['hook_suffix'] : null;

if (isset($conf['variable'])) {
global $$conf['variable'];
}

$conditions = array(
'variable' => isset($conf['variable']) && (!isset($$conf['variable']) || !$$conf['variable']),
'hook_suffix' => isset($conf['hook_suffix']) && (is_null($hook_suffix) || $conf['hook_suffix'] != $hook_suffix)
);

return in_array(true, array_values($conditions), true);
}

linter 对此感到不安:

if (isset($conf['variable'])) {
global $$conf['variable']; # What the heck is this?
}

我可以检查 PHP 错误日志并在服务器上重现相同的错误:PHP 解析错误:语法错误,意外的“[”,应为“,”或“;”

众所周知,此 WordPress 主题在过去有效,但在过去 3 年中似乎已被废弃。

让我进入问题的核心:global $$conf['variable']; 到底应该做什么。我假设这是已弃用的代码,因为它以前工作过。

最佳答案

这看起来像一个动态变量。我认为它正在尝试使用 $conf['variable'] 的名称将变量全局化。

你可以像这样修复错误,你只需要用花括号把它括起来:

global ${$conf['variable']};

这确实在较新版本的 PHP 7 中进行了更新/更改,如下所示:

http://php.net/manual/en/migration70.incompatible.php#migration70.incompatible.variable-handling.indirect

Changes to the handling of indirect variables, properties, 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 evaluation has changed.

            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']() |

Code that used the old right-to-left evaluation order must be rewritten to explicitly use that evaluation order with curly braces (see the above middle column). This will make the code both forwards compatible with PHP 7.x and backwards compatible with PHP 5.x.

关于数组值上的 PHP 全局关键字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53860618/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com