問題描述
我正在嘗試在 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_dump
和print_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_children
、xdebug.var_display_max_data
和 xdebug.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 調試變量失敗?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!