1 回答
TA贡献1777条经验 获得超10个赞
我没有找到一个内置的打印功能,它在包含具有关闭连接的 mysqli 对象时不会导致错误/警告(我实际上将其归类为错误/不需要的行为)。
我们结束了加入的Symfony Vardumper通过
composer require symfony/var-dumper
并编写一个小辅助函数来显示来自 cli 脚本或浏览器的正确和漂亮的输出:
use Symfony\Component\VarDumper\Cloner\VarCloner;
use Symfony\Component\VarDumper\Dumper\CliDumper;
use Symfony\Component\VarDumper\Dumper\HtmlDumper;
class Debug {
/**
* Method provides a function which can handle all our corner-cases for producing
* debug output.
*
* The corner cases are:
* - objects with recursion
* - mysqli references (also to closed connections in error handling)
*
* The returned result will be:
* - formatted for CLI if the script is run from cli
* - HTML formatted otherwise
* - The HTML formatted output is collapsed by default. Use CTRL-left click to
* expand/collapse all children
* - You can force html|cli formatting using the optional third parameter
*
* Uses the Symfony VarDumper composer module.
*
* @see https://github.com/symfony/var-dumper
* @see https://stackoverflow.com/questions/57520457/how-to-get-proper-debug-context-from-production-php-code-print-r-vs-var-export
* @param mixed $val - variable to be dumped
* @param bool $return - if true, will return the result as string
* @param string|null $format null|cli|html for forcing output format
* @return bool|string
*/
public static function varDump($val, $return = false, $format = null) {
if (is_null($format)) {
$format = php_sapi_name() == 'cli' ? 'cli' : 'html';
}
$cloner = new VarCloner();
if ($format === 'cli') {
$dumper = new CliDumper();
} else {
$dumper = new HtmlDumper();
}
$output = fopen('php://memory', 'r+b');
$dumper->dump($cloner->cloneVar($val), $output);
$res = stream_get_contents($output, -1, 0);
if ($return) {
return $res;
} else {
echo $res;
return true;
}
}
}
那个方法
可以处理我传递给它的所有输入而没有错误或警告
CLI 和 HTML 的格式都很好
将结果作为字符串返回,以便将其转发到外部错误跟踪系统,如哨兵
所以它勾选了我在最初问题中要求的所有方框。
感谢@BlackXero 正确理解问题并将我指向正确的方向。
- 1 回答
- 0 关注
- 190 浏览
添加回答
举报