使用带有循环引用的 print_r 和 var_dump [英] Using print_r and var_dump with circular reference

查看:34
本文介绍了使用带有循环引用的 print_r 和 var_dump的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 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 致命错误:第 10 行的 print_r_test.php 中允许的内存大小为 134217728 字节(试图分配 523800 字节)

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆