TransWikia.com

Magento 2 : print_r or var_dump is not working

Magento Asked by zed Blackbeard on July 30, 2020

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

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(
    MagentoFrameworkModelContext $context,
    MagentoFrameworkRegistry $registry,
    MagentoFrameworkApiExtensionAttributesFactory $extensionFactory,
    AttributeValueFactory $customAttributeFactory,
    MagentoStoreModelStoreManagerInterface $storeManager,
    MagentoCatalogApiCategoryAttributeRepositoryInterface $metadataService,
    MagentoCatalogModelResourceModelCategoryTree $categoryTreeResource,
    MagentoCatalogModelResourceModelCategoryTreeFactory $categoryTreeFactory,
    MagentoStoreModelResourceModelStoreCollectionFactory $storeCollectionFactory,
    MagentoFrameworkUrlInterface $url,
    MagentoCatalogModelResourceModelProductCollectionFactory $productCollectionFactory,
    MagentoCatalogModelConfig $catalogConfig,
    MagentoFrameworkFilterFilterManager $filter,
    IndexerCategoryFlatState $flatState,
    MagentoCatalogUrlRewriteModelCategoryUrlPathGenerator $categoryUrlPathGenerator,
    UrlFinderInterface $urlFinder,
    MagentoFrameworkIndexerIndexerRegistry $indexerRegistry,
    CategoryRepositoryInterface $categoryRepository,
    MagentoFrameworkModelResourceModelAbstractResource $resource = null,
    MagentoFrameworkDataCollectionAbstractDb $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).

Correct answer by Marius on July 30, 2020

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

Answered by luenib on July 30, 2020

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

Answered by matinict on July 30, 2020

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

Answered by shah dhairya on July 30, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP