【发布时间】:2017-12-08 13:26:01
【问题描述】:
PHP 调试工具kint 有一种奇怪的语法,其中某些符号可以作为函数的前缀来改变它们的行为,如this guide 所示。
相关信息:
修饰符是一种无需使用其他函数即可更改 Kint 输出的方法。只需在调用 kint 前加上修饰符即可应用它:
! Expand all data in this dump automatically
+ Disable the depth limit in this dump
- Attempt to clear any buffered output before this dump
@ Return the output of this dump instead of echoing it
~ Use the text renderer for this dump
Example:
+Kint::dump($data); // Disabled depth limit
!d($data); // Expanded automatically
这是如何工作的?
通过查看源代码,这些符号似乎被解析为一个名为$modifiers 的数组。但是你怎么能用 PHP 做到这一点呢?这个范围是什么,我可以用其他 unicode 符号来做吗,还是只有五个有问题的(+、-、~、!、@)。
“@”在 PHP 中已经有了前缀,请参阅:What is the use of the @ symbol in PHP?。这怎么能被推翻?
编辑:对给出的答案的后续问题是 kint 究竟如何弯曲 (php) 规则。例如为什么~ 没有给出语法错误。考虑这个例子:
<?php
function d($args) {
echo $args[0];
}
d([1,2,3]); // prints 1
~d([1,2,3]); // syntax error, unsupported operand types
对
<?php
require 'kint.php';
~d([1,2,3]); // prints the array with the text renderer with no issues
编辑 2:删除了 kint 使用 eval() 的未经证实的声明
【问题讨论】:
-
Kint 不使用
eval:github.com/kint-php/kint/search?q=eval