PHP 调试工具kint有一个奇怪的语法,其中某些符号可以作为函数的前缀来改变它们的行为,如本指南所示。
相关资料:
修饰符是一种无需使用其他函数即可更改 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 中已经有了用处,请参阅: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() 的未经证实的说法