I have a block php file. Block File Code:


namespace Vendor\SampleModule1\Block;

use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;
use Vendor\SampleModule1\Model\ResrouceModel\Item\Collection;
use Vendor\SampleModule1\Model\ResrouceModel\Item\CollectionFactory;

class Hello extends Template
    private $collectionFactory;

    public function __construct(
        Context $context,
        CollectionFactory $collectionFactory,
        array $data =[]
        $this->collectionFactory = $collectionFactory;
        parent::__construct($context, $data);

     * @return \Vendor\SampleModule1\Model\Item[]
    public function getItems()
        return $this->collectionFactory->create()->getItems();

When I try to see the view the error is

Fatal error: Uncaught TypeError: Argument 1 passed to Vendor\SampleModule1\Block\Hello::__construct() must be an instance of Magento\Framework\View\Element\Template\Context, instance of Magento\Framework\ObjectManager\ObjectManager given ....

How is that possible ? ? ?

  • try to do di:compile – Keyur Shah Sep 25 '17 at 6:44
  • Did you try to remove var/generation folder? – Khoa TruongDinh Sep 25 '17 at 6:44

You need to compile your code after you have done changes.

Please do

php bin/magento setup:di:compile

| improve this answer | |
  • Thank you, I will try it. It wasn't mentioned at all in the tutorial. – Lachezar Raychev Sep 25 '17 at 6:57
  • Yes, they are not going to do that coz, they consider it as an "already knowing" fact (by every Magento developer). We all gone through this confusion and found out the answer ourself :) – Rajeev K Tomy Sep 25 '17 at 7:00
  • but on every tutorial the commande chace:flush is used :) whatever ... just weird, thank you – Lachezar Raychev Sep 25 '17 at 7:06
  • 2
    @LachezarRaychev Check this answer for when you should run which commands: magento.stackexchange.com/a/184927/35758 – Prince Patel Sep 25 '17 at 7:54
  • 1
    @PrincePatel Yes, that's a nice reference point. – Rajeev K Tomy Sep 25 '17 at 7:58

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.