php 7.2 中未定义的常量错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48236765/
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
Undefined constant error in php 7.2
提问by Ruzo
I have theses errors in php v7.2
but don't see any E_WARNING
when using php v7.1
.
我在 php 中有这些错误,v7.2
但E_WARNING
在使用 php 时没有看到任何错误v7.1
。
How can I resolve following errors?
如何解决以下错误?
/web13/web/boutique/includes/Sites/Shop/NavigationHistory.php on line 39 [12-Jan-2018 22:44:20 America/Toronto] PHP Warning: Use of undefined constant MODULE_HEADER_SELECT_TEMPLATE_STATUS - assumed 'MODULE_HEADER_SELECT_TEMPLATE_STATUS' (this will throw an Error in a future version of PHP) in /var/www/clients/client1/web13/web/boutique/includes/Sites/Shop/Template.php on line 356
/web13/web/boutique/includes/Sites/Shop/NavigationHistory.php 第 39 行 [12-Jan-2018 22:44:20 America/Toronto] PHP 警告:使用未定义的常量 MODULE_HEADER_SELECT_TEMPLATE_STATUS - 假设 'MODULE_HEADER_SELECT_US_TEMPLATE在第 356 行的 /var/www/clients/client1/web13/web/boutique/includes/Sites/Shop/Template.php 中在未来版本的 PHP 中抛出错误)
回答by Aniket Sahrawat
This is a common warning that occurs whenever PHP has detected the usage of an undefined constant.
这是一个常见的警告,每当 PHP 检测到使用了未定义的常量时就会发生。
Here is an example of constant being defined in PHP:
这是在 PHP 中定义的常量示例:
define('PI', 3.14);
Below is a list of some cases that might cause the issue:
以下是可能导致问题的一些情况的列表:
Forgetting to use a $ symbol at the start of a variable name.
$name = "Aniket"; echo name; // forgot to add $ before name
The above code will throw: Notice: Use of undefined constant name – assumed ‘name'. Because there is no dollar sign in front of the variable “name”, PHP assumes that I was trying to reference a constant variable called “name”.
Forgetting to place quotes around strings.
echo $_POST[email];
In the example above, I failed to place quotes around the
$_POST
variable“email”
. This code will throw: Notice: Use of undefined constant name – assumed 'email'.To fix this, I'd obviously have to do the following:
echo $_POST["email"];
忘记在变量名的开头使用 $ 符号。
$name = "Aniket"; echo name; // forgot to add $ before name
上面的代码将抛出:注意:使用未定义的常量名称 - 假定为 'name'。因为变量“name”前面没有美元符号,PHP 假定我试图引用一个名为“name”的常量变量。
忘记在字符串周围放置引号。
echo $_POST[email];
在上面的例子中,我没有在
$_POST
变量周围加上引号“email”
。此代码将抛出:注意:使用未定义的常量名称 - 假定为 'email'。要解决此问题,我显然必须执行以下操作:
echo $_POST["email"];
According to Deprecated features in PHP 7.2.xyou should not use undefined constants because:
根据PHP 7.2.x 中已弃用的功能,您不应使用未定义的常量,因为:
Unquoted strings that are non-existent global constants are taken to be strings of themselves.
This behaviour used to emit an E_NOTICE, but will now emit an E_WARNING. In the next major version of PHP, an Error exception will be thrown instead.
作为不存在的全局常量的未加引号的字符串被视为其自身的字符串。
此行为过去会发出E_NOTICE,但现在会发出E_WARNING。在 PHP 的下一个主要版本中,将抛出 Error 异常。
You can prevent this E_WARNING
only if you declare the constant value before using it.
E_WARNING
只有在使用常量值之前声明常量值,才能防止这种情况发生。
In the above question, MODULE_HEADER_SELECT_TEMPLATE_STATUS
is not defined.
在上面的问题中,MODULE_HEADER_SELECT_TEMPLATE_STATUS
是没有定义的。
回答by Wale
In addition, for those who are new to wordpress Plugin development and cant seem to figure out what it means to define "Constant" before its used...
此外,对于那些不熟悉 wordpress 插件开发并且似乎无法弄清楚在使用之前定义“常量”意味着什么的人......
Here is an example of what will throw an error:
以下是将引发错误的示例:
add_action('wp_enqueue_scripts', myprefix_load_styles);
Declaring a function directly like that in a hook is one way to generate an error like this. Instead:
像在钩子中那样直接声明一个函数是产生这样的错误的一种方式。反而:
add_action('wp_enqueue_scripts', 'myprefix_load_styles');
Note, the function name is now inside the quotes. And that should work, if thats your scenario.
请注意,函数名称现在位于引号内。如果那是您的情况,那应该可行。
回答by engine9pw
I've just created a PHP job to recursively clean up all files in a PHP project and automatically quote all strings which are undefined constants used inside square brackets for the array syntax.
我刚刚创建了一个 PHP 作业来递归清理 PHP 项目中的所有文件,并自动引用所有字符串,这些字符串是方括号内用于数组语法的未定义常量。
Observation: this fix only targets array usage like $a[key1] which will be automatically transformed into $a['key1']. The clean up process DOES NOT parse and compute a list of defined constants in your project, in order to white-list them for usage without quotes in all possible contexts.
观察:此修复仅针对像 $a[key1] 这样的数组使用,它会自动转换为 $a['key1']。清理过程不会解析和计算项目中已定义常量的列表,以便在所有可能的上下文中将它们列入白名单以供使用而无需引号。
It's recommended to run it for your project on DEV first, check functionality and then push to LIVE.
建议先在 DEV 上为您的项目运行它,检查功能,然后推送到 LIVE。
QUICK USAGE:
快速使用:
git clone https://github.com/eyroot/lx-utils lx-utils
cd lx-utils && composer install --no-dev
php run/cleanUpSquareBrackets.php /path/you/want/to/clean/up
The complete usage instructions and source code are on the page:
https://github.com/eyroot/lx-utils
完整的使用说明和源代码在页面上:https:
//github.com/eyroot/lx-utils