沃梦达 / 编程问答 / php问题 / 正文

什么会导致 print_r 和/或 var_dump 调试变量失败?

What would cause a print_r and/or a var_dump to fail debugging a variable?(什么会导致 print_r 和/或 var_dump 调试变量失败?)

本文介绍了什么会导致 print_r 和/或 var_dump 调试变量失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Magento 中调试 PayPal 审核流程.每次我尝试转储以下变量时,我都会得到一个白页:

I'm attempting to debug the PayPal review process in Magento. Every time I try to dump the following variable I get a white page:

//the variable declaration:
$shippingAddress = $this->getShippingAddress();

//the dump that breaks the page:
 <?php echo '<pre>';print_r($shippingAddress);echo '</pre>'; ?>

我还尝试在页面上使用一个变量,该变量用于 if 语句以外的其他内容.

I also tried with a variable on the page that was being used for something other than if statements.

//this variable displays results
<?php echo '<pre>';print_r($billingBlock->setShowAsShippingCheckbox(true)->toHtml());echo '</pre>'; ?>

//however, this one does not:
<?php echo '<pre>';print_r($billingBlock);echo '</pre>'; ?>

我只是想知道什么可能导致我的 var_dump 破坏页面?如果无法转储,我如何查看对象中的内容?

I was just wondering what might cause my var_dump to break the page? How do I see what is in the object if I can't dump it?

推荐答案

首先,PHP 从不只是白页".当你看到一个空白屏幕时,这意味着 PHP 的执行由于某种原因停止了.但是,除非您的服务器已配置为不记录错误,否则 PHP 错误日志或 Magento 异常日志应该会为您提供错误信息.

First, PHP never "just white pages". When you get a blank screen, that means PHP's execution has halted fro some reason. However, unless your server has been configured to not log errors, the PHP error log or the Magento exception log should have an error for you.

就您的具体问题而言,Magento 的许多对象都包含对大量信息的引用——有时这些引用是循环的.PHP 的var_dumpprint_r 函数会盲目地遵循这些循环引用并试图将所有内容打印出来.这最终会导致 PHP 使用比 memory_limit ini 设置允许的更多的内存,并且执行停止.

As far as your specific problem goes, many of Magento's objects contain reference to a large amount of information — and sometimes the references are circular. PHP's var_dump and print_r functions will blindly follow these circular references and attempt to print everything out. This eventually leads to PHP using more memory than is allowed by the memory_limit ini setting, and execution halts.

大多数 PHP 专业人员使用 xDebug 扩展来解决这个问题.xDebug 扩展有一个修改过的 var_dump,它将限制转储的信息量,从而防止上述内存限制问题.如果 xDebug 仍然没有帮助,xdebug.var_display_max_childrenxdebug.var_display_max_dataxdebug.var_display_max_depth ini 设置是您想要调整的设置与内存限制问题.(一些 PHP 发行版最初将这些设置得太高)

Most PHP professionals use the xDebug extension to work around this. The xDebug extension has a modified var_dump that will limit the amount of information dumped, which prevents the above memory limit problems. The xdebug.var_display_max_children, xdebug.var_display_max_data, and xdebug.var_display_max_depth ini settings are the ones you'll want to tweak if xDebug's still not helping with the memory limit problem. (some PHP distributions have these set too high initially)

如果这不是可能的,那么对 var_dump 稍微小心一点还是有帮助的.

If that's not a possibility, a little caution with your var_dump's can still help.

用它来找出变量类型

var_dump(get_class($thing));

如果它是一个 Magento 对象,使用它来查看它的数据键

If it's a Magento object, use this to see its data keys

var_dump(array_keys($thing->getData()));

然后用

var_dump($thing->getData('key_name'));
var_dump($thing->getKeyName()));

这篇关于什么会导致 print_r 和/或 var_dump 调试变量失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:什么会导致 print_r 和/或 var_dump 调试变量失败?

基础教程推荐