简体   繁体   English

PHP - 非递归var_dump?

[英]PHP - non-recursive var_dump?

When dealing with certain PHP objects, it's possible to do a var_dump() and PHP prints values to the screen that go on and on and on until the PHP memory limit is reached I assume. 在处理某些PHP对象时,可以执行var_dump()并且PHP将值打印到屏幕上,然后继续打开,直到达到PHP内存限制为止。 An example of this is dumping a Simple HTML DOM object. 一个例子是转储Simple HTML DOM对象。 I assume that because you are able to traverse children and parents of objects, that doing var_dump() gives infinite results because it finds the parent of an object and then recursively finds it's children and then finds all those children's parents and finds those children, etc etc etc. It will just go on and on. 我假设因为你能够遍历对象的子对象和父对象,所以做var_dump()会给出无限的结果,因为它找到一个对象的父对象,然后递归地找到它的子对象然后找到所有这些孩子的父对象并找到那些孩子等等。等等。它会继续下去。

My question is, how can you avoid this and keep PHP from dumping recursively dumping out the same things over and over? 我的问题是,你怎么能避免这种情况并让PHP不要一遍又一遍地倾销掉同样的东西? Using the Simple HTML DOM parser example, if I have a DOM object that has no children and I var_dump() it, I'd like it to just dump the object and no start traversing up the DOM tree and dumping parents, grandparents, other children, etc. 使用Simple HTML DOM解析器示例,如果我有一个没有子节点的DOM对象而且我有var_dump()它,我希望它只是转储对象而没有开始遍历DOM树并转储父母,祖父母,其他孩子等

Install XDebug extension in your development environment. 在开发环境中安装XDebug扩展。 It replaces var_dump with its own that only goes 3 members deep by default. 它将var_dump替换为自己的var_dump,默认情况下只有3个成员。

https://xdebug.org/docs/display https://xdebug.org/docs/display

It will display items 4 levels deep as an ellipsis. 它将显示4个级别的项目作为省略号。 You can change the depth with an ini setting. 您可以使用ini设置更改深度。

All PHP functions: var_dump, var_export, and print_r do not track recursion / circular references. 所有PHP函数:var_dump,var_export和print_r都不跟踪递归/循环引用。

Edit: 编辑:

If you want to do it the hard way, you can write your own function 如果你想以艰难的方式去做,你可以编写自己的功能

print_rr($thing, $level=0) {
   if ($level == 4) { return; }
   if (is_object($thing)) {
       $vars = get_object_vars($thing);

   }

   if (is_array($thing)) {
       $vars = $thing;
   }
   if (!$vars) {
       print " $thing \n";
       return;
   }

   foreach ($vars as $k=>$v) {
      if (is_object($v)) return print_rr($v, $level++);
      if (is_array($v)) return print_rr($v, $level++);
      print "something like var_dump, var_export output\n";
   }
}

Why don't you simply run a foreach loop on your object? 为什么不简单地在对象上运行foreach循环?

From the PHP docs : PHP文档

The foreach construct simply gives an easy way to iterate over arrays. foreach构造简单地提供了一种迭代数组的简单方法。 foreach works only on arrays ( and objects ), and will issue an error when you try to use it on a variable with a different data type or an uninitialized variable. foreach仅适用于数组( 和对象 ),并且当您尝试在具有不同数据类型或未初始化变量的变量上使用它时将发出错误。

I had this problem and didn't need to see inside the objects, just the object classnames, so I wrote a simple function to replace objects with their classnames before dumping the data: 我遇到了这个问题并且不需要查看对象内部,只需要查看对象类名,因此我在转储数据之前编写了一个简单的函数来替换对象及其类名:

function sanitizeDumpContent($content)
{
    if (is_object($content)) return "OBJECT::".get_class($content);

    if (!is_array($content)) return $content;

    return array_map(function($node) {
        return $this->sanitizeDumpContent($node);
    }, $content);
}

Then, when you want to dump something, just do this: 然后,当你想要转储某些东西时,只需这样做:

var_dump(sanitizeDumpContent($recursive_content))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM