Magento 2 : print_r or var_dump is not working

This one is crazy with magento because sometimes it works and sometimes it just shows blank page. Once it gave me a real hard time. Finally found solution somewhere.

$_product = $block->getProduct();
echo "<pre>"; print_r($_product->debug());die("dead");

print_r and var_dump does not play well when there are a lot of object involved.
Both of them try to print the category object and this object has a lot of objects attached to it because of the dependency injection

If you take a look at the constructor from the category model you will see this:

public function __construct(
    \Magento\Framework\Model\Context $context,
    \Magento\Framework\Registry $registry,
    \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory,
    AttributeValueFactory $customAttributeFactory,
    \Magento\Store\Model\StoreManagerInterface $storeManager,
    \Magento\Catalog\Api\CategoryAttributeRepositoryInterface $metadataService,
    \Magento\Catalog\Model\ResourceModel\Category\Tree $categoryTreeResource,
    \Magento\Catalog\Model\ResourceModel\Category\TreeFactory $categoryTreeFactory,
    \Magento\Store\Model\ResourceModel\Store\CollectionFactory $storeCollectionFactory,
    \Magento\Framework\UrlInterface $url,
    \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
    \Magento\Catalog\Model\Config $catalogConfig,
    \Magento\Framework\Filter\FilterManager $filter,
    Indexer\Category\Flat\State $flatState,
    \Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator $categoryUrlPathGenerator,
    UrlFinderInterface $urlFinder,
    \Magento\Framework\Indexer\IndexerRegistry $indexerRegistry,
    CategoryRepositoryInterface $categoryRepository,
    \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
    \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
    array $data = []
) {
    $this->metadataService = $metadataService;
    $this->_treeModel = $categoryTreeResource;
    $this->_categoryTreeFactory = $categoryTreeFactory;
    $this->_storeCollectionFactory = $storeCollectionFactory;
    $this->_url = $url;
    $this->_productCollectionFactory = $productCollectionFactory;
    $this->_catalogConfig = $catalogConfig;
    $this->filter = $filter;
    $this->flatState = $flatState;
    $this->categoryUrlPathGenerator = $categoryUrlPathGenerator;
    $this->urlFinder = $urlFinder;
    $this->indexerRegistry = $indexerRegistry;
    $this->categoryRepository = $categoryRepository;
    parent::__construct(
        $context,
        $registry,
        $extensionFactory,
        $customAttributeFactory,
        $storeManager,
        $resource,
        $resourceCollection,
        $data
    );
}

Each object passed to the constructor is referenced in the category model.
And each object may have additional objects linked to it.
In conclusion print_r and var_dump won't work for most of the objects in Magento 2.
Solutions:

  • use xdebug to see how the object looks like.
  • print only $category->getData() to see the actual properties of the category object (but this might not always work either).

I solve my var_dump(array) result in a 500 error in Magento2 issue as below:

//app/code/MyCompany/Shipping/view/adminhtml/templates/order/view/items.phtml

foreach ($_items as $_item):
    echo "<pre>";
    var_dump($_item->debug());
    echo "</pre>";

Output as Below:

enter image description here

Tags:

Php

Magento2