使用带循环引用的print_r和var_dump [英] Using print_r and var_dump with circular reference
问题描述
我正在使用 MVC框架Symfony ,而且似乎很多内置我想调试的对象有循环引用。这使得不可能使用 print_r()
或 var_dump()
打印变量(因为它们遵循循环引用广告无限或者直到过程用尽内存(以先到者为准)。
I'm using the MVC framework Symfony, and it seems a lot of the built-in objects I want to debug have circular references. This makes it impossible to print the variables with print_r()
or var_dump()
(since they follow circular references ad infinitum or until the process runs out of memory, whichever comes first).
而不是将自己的 print_r
克隆与一些情报,那里有更好的选择吗?我只想能够将变量(对象,数组或标量)打印到日志文件,HTTP头或网页本身。
Instead of writing my own print_r
clone with some intelligence, are there better alternatives out there? I only want to be able to print a variable (object, array or scalar), either to a log file, http header or the web page itself.
编辑:至澄清问题是什么,请尝试此代码:
to clarify what the problem is, try this code:
<?php
class A
{
public $b;
public $c;
public function __construct()
{
$this->b = new B();
$this->c = new C();
}
}
class B
{
public $a;
public function __construct()
{
$this->a = new A();
}
}
class C
{
}
ini_set('memory_limit', '128M');
set_time_limit(5);
print_r(new A());
#var_dump(new A());
#var_export(new A());
它不适用于 print_r()
, var_dump()
或 var_export()
。错误消息是:
It doesn't work with print_r()
, var_dump()
or var_export()
. The error message is:
PHP致命错误:允许的内存大小为134217728个字节(尝试分配523800个字节)在print_r_test.php中第10行
PHP Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 523800 bytes) in print_r_test.php on line 10
推荐答案
我们正在使用PRADO框架,它有一个内置的类 TVarDumper可以很好地处理这些复杂的对象 - 甚至可以将它格式化在很好的HTML中。语法突出显示。您可以从 HERE 。
We are using the PRADO Framework and it has a built in class called "TVarDumper" which can handle such complex objects pretty well - it even can format it in nice HTML incl. Syntax Highlighting. You can get that class from HERE.
这篇关于使用带循环引用的print_r和var_dump的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!