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, 2017 at 6:44
  • Did you try to remove var/generation folder? Sep 25, 2017 at 6:44

1 Answer 1


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

Please do

php bin/magento setup:di:compile

  • Thank you, I will try it. It wasn't mentioned at all in the tutorial. Sep 25, 2017 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 :) Sep 25, 2017 at 7:00
  • but on every tutorial the commande chace:flush is used :) whatever ... just weird, thank you Sep 25, 2017 at 7:06
  • 2
    @LachezarRaychev Check this answer for when you should run which commands: magento.stackexchange.com/a/184927/35758 Sep 25, 2017 at 7:54
  • 1
    @PrincePatel Yes, that's a nice reference point. Sep 25, 2017 at 7:58

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.