PHP eval():: 使用未定义常量 not_a_constant- 假设 'not_a_constant' php 7.2
PHP eval():: Use of undefined constant not_a_constant- assumed 'not_a_constant' php 7.2
这段代码给出了标题的警告。
最简单的解决方案是将单引号添加到 "not_a_constant"。
然而,这打破了 "eval"
我试过字符串连接等
不应删除 eval。
不应使用辅助变量。
eval('$some_defined_var[not_a_constant] .= "' . some_function('some_string') . '";')
请尝试使用 php 7.2 定义一些哑变量和函数的代码,以检查警告和可能的解决方案。
编辑
1)代码标准禁止对 string.s 使用双引号
2)我们试图避免在 "not_a_constant" 中的索引内转义单引号。
这意味着,我想要实现的是:
$some_defined_var['not_a_constant']
如果不能转义单引号,禁止使用双引号,就剩下heredoc和nowdoc了。 manual 向您展示了如何使用它们。对于您的代码,您可以使用:
<?php
$some_defined_var['not_a_constant'] = 'old ';
function some_function($a) { return 'New '.$a;}
eval( <<<'EOE'
$some_defined_var['not_a_constant'] .= "
EOE
. some_function('some_string') . '";'
);
var_dump($some_defined_var);
请注意,我通常不建议编写这样的代码。我只是在应用你的限制。我完全同意所有不喜欢你的编码标准的评论员。
基于此评论:
Inside the indexing, yes, trying to avoid escaping. Outside, in the concatenation, I may use double quotes, escaping, just not inside the indexing.
我认为其中之一可能符合您相当神秘的规则:
eval('$some_defined_var[' . "'not_a_constant'" . '] .= "' . some_function('some_string') . '";')
eval("$some_defined_var['not_a_constant'] .= \"" . some_function('some_string') . '";')
关于您实际使用 eval
的原因,感觉这里缺少一些上下文,因为您给出的示例可以重写为:
$some_defined_var['not_a_constant'] = (string)some_function('some_string');
或者可能是:
$some_defined_var['not_a_constant'] = eval('"'. some_function('some_string') . '"');
因为 some_function
可以 return 代码在 eval
时做任何它想做的事情,例如
function some_function($who_cares) {
return '"; var_dump($config["database_password"]); "';
}
如果我们知道哪些部分是动态的,那么关于双引号等的规则可能有意义,因为它可能是为了保护 eval
;但坦率地说,确保 eval
安全的唯一方法就是不使用它。
这段代码给出了标题的警告。 最简单的解决方案是将单引号添加到 "not_a_constant"。 然而,这打破了 "eval" 我试过字符串连接等
不应删除 eval。 不应使用辅助变量。
eval('$some_defined_var[not_a_constant] .= "' . some_function('some_string') . '";')
请尝试使用 php 7.2 定义一些哑变量和函数的代码,以检查警告和可能的解决方案。
编辑 1)代码标准禁止对 string.s 使用双引号 2)我们试图避免在 "not_a_constant" 中的索引内转义单引号。 这意味着,我想要实现的是:
$some_defined_var['not_a_constant']
如果不能转义单引号,禁止使用双引号,就剩下heredoc和nowdoc了。 manual 向您展示了如何使用它们。对于您的代码,您可以使用:
<?php
$some_defined_var['not_a_constant'] = 'old ';
function some_function($a) { return 'New '.$a;}
eval( <<<'EOE'
$some_defined_var['not_a_constant'] .= "
EOE
. some_function('some_string') . '";'
);
var_dump($some_defined_var);
请注意,我通常不建议编写这样的代码。我只是在应用你的限制。我完全同意所有不喜欢你的编码标准的评论员。
基于此评论:
Inside the indexing, yes, trying to avoid escaping. Outside, in the concatenation, I may use double quotes, escaping, just not inside the indexing.
我认为其中之一可能符合您相当神秘的规则:
eval('$some_defined_var[' . "'not_a_constant'" . '] .= "' . some_function('some_string') . '";')
eval("$some_defined_var['not_a_constant'] .= \"" . some_function('some_string') . '";')
关于您实际使用 eval
的原因,感觉这里缺少一些上下文,因为您给出的示例可以重写为:
$some_defined_var['not_a_constant'] = (string)some_function('some_string');
或者可能是:
$some_defined_var['not_a_constant'] = eval('"'. some_function('some_string') . '"');
因为 some_function
可以 return 代码在 eval
时做任何它想做的事情,例如
function some_function($who_cares) {
return '"; var_dump($config["database_password"]); "';
}
如果我们知道哪些部分是动态的,那么关于双引号等的规则可能有意义,因为它可能是为了保护 eval
;但坦率地说,确保 eval
安全的唯一方法就是不使用它。