6

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.

8

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).
0
12

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

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

0

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, privacy policy and cookie policy

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