ntzm/icecream

甜美丝滑的打印调试

1.1.1 2019-09-05 15:16 UTC

This package is auto-updated.

Last update: 2024-09-06 02:10:01 UTC


README

A PHP port of Python's IceCream.

使用方法

use function IceCream\ic;

function foo($i) {
    return $i + 333;
}

ic(foo(123));
// Outputs:
// ic| foo(123): 456

ic(1 + 5);
// Outputs:
// ic| 1 + 5: 6

ic(foo(123), 1 + 5);
// Outputs:
// ic| foo(123): 456, 1 + 5: 6

function bar() {
    ic();
}
bar();
// Outputs:
// ic| example.php:18 in bar()

安装

$ composer require --dev ntzm/icecream

配置

如果您想禁用输出,可以调用 IceCream::disable()。如果您想重新启用输出,可以调用 IceCream::enable()

如果您想更改输出的前缀,可以调用 IceCream::setPrefix('myPrefix: ')(默认前缀为 ic| )。

如果您想更改输出结果的方式,可以调用 IceCream::setOutputFunction()。例如,如果您想将消息记录到文件中

IceCream::setOutputFunction(function (string $message): void {
    file_put_contents('log.txt', $message . PHP_EOL, FILE_APPEND);
});

您可以通过调用 IceCream::resetOutputFunction() 重置为默认的输出函数。

注意事项

  • 您应在每行中只调用一次 ic,否则您将得到错误的输出
// Don't do this
ic('foo'); ic('bar');
  • 您不应将 ic 函数别名
// Don't do this
use function IceCream\ic as debug;
debug();
  • 您不应动态使用 ic 函数
// Don't do this
$fn = 'IceCream\ic';
$fn();