Why print_r and var_dump is not working in magento 2 ? I have check with category object in my custom module block and .phtml files. This does not show anything.

4 Answers 4


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;

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.

  • 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).

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");

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


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

Output as Below:

enter image description here


I'm dumping the content of a carrier's response, so what I do is dump it as an array:

$this->collectCarrierRates('carrier_name', $request);
$this->_logger->info(print_r($this->getResult()->asArray(), true));

I hope it works with a category object.

I'm using 2.3.3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.