从 PHP 7.0 切换到 7.2 后未定义常量



我的网站托管商将很快将所有帐户从 PHP 7.0 切换到 PHP 7.2。所以我安装了我当前WordPress安装的克隆,以便用PHP 7.2进行测试。当我激活 PHP 7.0 时,一切正常。但是当我切换到 PHP 7.2 时,我从其中一个插件收到一条奇怪的错误消息。

Warning: Use of undefined constant TABBER_TABS_DIR - assumed 'TABBER_TABS_DIR' (this will throw an Error in a future version of PHP) in /home/xeccbike/public_html/testing/wp-content/plugins/tabber-tabs-widget/tabber-tabs.php on line 31
30: / Set constant path to the plugin directory.
31: define( TABBER_TABS_DIR, plugins_url('tabber-tabs-widget/'));
32: 
33: // Load Language
34: load_plugin_textdomain('tabber-tabs-widget', false, TABBER_TABS_DIR . 'language');

有谁知道那可能是什么?任何提示都值得赞赏。谢谢

AJ

我也有。我只是按照警告消息的建议将常量放在单引号中(在您的情况下,将在第 31 行define( 'TABBER_TABS_DIR', plugins_url('tabber-tabs-widget/'));),这解决了它。

让我们看一下这个代码片段:

$string = test;
if ($string == 'test') {
echo "This is test string";
}

显然,您可以在此处看到未引号的字符串测试。在 PHP 7.1 中,脚本的输出为:

Notice: Use of undefined constant test - assumed 'test' in ...
This is test string

但在 PHP 7.2 中,输出将是:

Warning: Use of undefined constant test - assumed 'test' (this will throw an Error in a future version of PHP) in ...
This is test string

如您所见,这不是一个很大的变化(通知已更改为警告),但是在 PHP 8 中它可能会导致错误。

此更改的主要原因不仅仅是停止对未引号字符串的支持,而是避免在输入某些 PHP 关键字时出现严重问题。

我建议我建议阅读这篇文章。也许有用

最新更新