0

When I run setup:di:compile command in M2, I got this error in one of my modules.

Extra parameters passed to parent construct: $context

File name : edit.php
Location : /app/code/Vendor/Module/Controller/Adminhtml/Attachment/Edit.php

        <?php

            namespace Vendor\Module\Controller\Adminhtml\Attachment;

            use Magento\Framework\Controller\Result\JsonFactory;

            class Edit extends \Vendor\Module\Controller\Adminhtml\Attachment{

                protected $_resultPageFactory;
                protected $_resultJsonFactory;

                public function __construct(
                    \Magento\Backend\App\Action\Context $context,
                    \Magento\Framework\View\Result\PageFactory $resultPageFactory,
                    JsonFactory $resultJsonFactory,
                    \Vendor\Module\Model\AttachmentFactory $attachmentFactory,
                    \Magento\Framework\Registry $registry
                )
                {
                    $this->_resultPageFactory = $resultPageFactory;
                    $this->_resultJsonFactory = $resultJsonFactory;
                    parent::__construct($context, $resultPageFactory, $resultJsonFactory, $attachmentFactory, $registry);
                }

Which params are extra in this code ?

Please help. My other modules are not installing due to this error.

2
  • Please read my post again. Your parent class construct is inconsistent with your parent::__construct(). If you have updated the parent class, post it as well. Commented Feb 25, 2017 at 9:34
  • @PankajBhope, would you please post the code bcoz I am new in magento, I can not understand what is inconsistent in my which code. I have updated my code as your post. Point 1 and Point 2.
    – Prince
    Commented Feb 27, 2017 at 3:50

4 Answers 4

1

There could be any one of these issues.

1. Either the number of objects passed in the following parent construct

parent::__construct(arguments...)

are not matching with the parent class

public function __construct (arguments... ) { . . .

}

i.e. if the parents construct contains 4 arguments. Then after extending it if you are using

parent::__construct(4 arguments)

Pass the four arguments that are used in the parent class. Your current classes contruct might have more arguments.

2. Or the sequence of the arguments is wrong. i.e. if the parent's construct is like this

public function __construct (Class1 $Object1, Class2 $object2) { . . .

}

Then after extending it, in your class while using parent's construct in your current Construct method. The sequence should be same.

ex: if parent class is

class ParentClass
{
    public function __construct(
    Class1 $object1,
    Class2 $object2
    ) {

    }
}

then :

This is wrong

class MyClass extends ParentClass
{
     public function __construct(
     Class1 $object1,
     Class2 $object2,
     some more Objects .. . 
     ) {
        parent::__(object2, object1);
     }   
}

This is right

class MyClass extends ParentClass
{
     public function __construct(
     Class1 $object1,
     Class2 $object2,
     some more Objects .. . 
     ) {
        parent::__(object1, object2); // order and no. of arguments matters here. 
     }   
}

If this doesn't solve your issue. Please Post the parent class Vendor\Module\Controller\Adminhtml\Attachment I will provide the exact solution.

In your case:

Just replace

parent::__construct($attachmentFactory, $registry, $context);

with

parent::__construct($attachmentFactory, $registry, $resultRedirectFactory, $context);

and clear the generation folder.

If even this doesn't work. Maintain your sequence of arguments as in Parents (but this shouldn't have cause any issue.)

8
  • I have done like this, parent::__construct($context, $attachmentFactory, $registry); Error: Argument 1 passed to Vendor\Module\Controller\Adminhtml\Attachment::__construct() must be an instance of Vendor\Module\Model\AttachmentFactory, instance of Magento\Backend\App\Action\Context given, called in /app/code/Vendor/Module/Controller/Adminhtml/Attachment/Edit.php
    – Prince
    Commented Feb 18, 2017 at 12:35
  • I am updating the answer. Please look at the bottom lines. Commented Feb 18, 2017 at 12:43
  • and Do pass the $resultRedirectFactory in your construct. Commented Feb 18, 2017 at 12:47
  • @Prince - Does it resolved your issue. Commented Feb 19, 2017 at 2:41
  • Update code : parent::__construct($attachmentFactory, $registry, $resultRedirectFactory, $context); Run "setup:di:compile" command => Incompatible argument type: Required type: \Magento\Backend\App\Action\Context.
    – Prince
    Commented Feb 20, 2017 at 5:27
0

Change the order of your construct, First parameter should be Magento\Backend\App\Action\Context;

Check bellow example:

public function __construct(
    Context $context,
    BackendSession $backendSession,
    PageFactory $resultPageFactory,
    AttachmentFactory $attachmentFactory,
    Registry $registry
)
{
.....
}
6
  • By this updated code I got, Fatal Error : Argument 1 passed to Vendor\Module\Controller\Adminhtml\Attachment\Edit::__construct() must be an instance of Magento\Backend\App\Action\Context, instance of Magento\Backend\Model\Session\Interceptor given
    – Prince
    Commented Feb 18, 2017 at 11:57
  • please post the construct of Vendor\Module\Controller\Adminhtml\Attachment.php class. For getting more clarity. Commented Feb 18, 2017 at 12:09
  • @Prince after change, delete var/generation/* OR generation/*(V2.1.4)
    – Sohel Rana
    Commented Feb 18, 2017 at 12:12
  • @SohelRana, fatal error solved but when I run compile command, error is still exist.
    – Prince
    Commented Feb 18, 2017 at 12:20
  • @PankajBhope, for Attachment.php , please click on magento.stackexchange.com/questions/160054/… , I have asked question first for Attachment.php
    – Prince
    Commented Feb 18, 2017 at 12:22
0

Btw, this issue wouldn't happen with Magento classes. Because in Magento all new dependencies are added in backward compatible way.

For adding new dependency we follow next rules:

  1. Introduce new private property
  2. Add new optional parameter to constructor the end of arguments list
  3. In constructor body, if new dependency is not provided (value of introduced argument is null) fetch dependency with Magento\Framework\App\ObjectionManager::getInstance()
  4. Use private property in class implementation to access new dependency

class ExistingClass
{
    /** @var \New\Dependency\Interface */
    private $newDependency;

    public function __construct(
        \Old\Dependency\Intreface $oldDependency,
        $oldRequiredConstructorParameter,
        $oldOptinalConstructorParameter = null,
        \New\Dependency\Interface $newDependency = null
    ) {
        ...
        $this->newDependency = $newDependency ?: \Magento\Framework\App\ObjectManager::getInstance()->get(\New\Dependency\Interface::class);
    }

    public function existingFunction()
    {
        // Existing functionality
        ...
        ...

        // Use $this->newDependency wherever the new dependency is needed
        ...
        ...
    }
}
0

Ok, So referencing to Attachment.php (modified a little, so copy this)

<?php
namespace Vendor\Module\Controller\Adminhtml;

use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\Registry;
use Vendor\Module\Model\AttachmentFactory;

abstract class Attachment extends Action
{

    protected $_attachmentFactory;           
    protected $_coreRegistry;

    public function __construct(
        Context $context,            
        AttachmentFactory $attachmentFactory,
        Registry $coreRegistry

    ) {
        $this->_attachmentFactory     = $attachmentFactory;
        $this->_coreRegistry          = $coreRegistry;
        parent::__construct($context);
    }


    protected function _initAttachment()
    {
        $attachmentId  = (int)$this->getRequest()->getParam('attachment_id');

        $attachment    = $this->_attachmentFactory->create();
        if ($attachmentId) {
            $attachment->load($attachmentId);
        }
        $this->_coreRegistry->register('solwin_productattachment_attachment', $attachment);
        return $attachment;
    }
}

Modify your Edit.php like this:

    namespace Vendor\Module\Controller\Adminhtml\Attachment;

    use Magento\Framework\Controller\Result\JsonFactory;
    use Magento\Backend\App\Action\Context;
    use Magento\Framework\Registry;
    use Vendor\Module\Model\AttachmentFactory;
    use Magento\Framework\View\Result\PageFactory;

    class Edit extends \Vendor\Module\Controller\Adminhtml\Attachment{

        protected $_resultPageFactory;
        protected $_resultJsonFactory;

        public function __construct(
            Context $context,
            AttachmentFactory $attachmentFactory,
            Registry $coreRegistry,
            PageFactory $resultPageFactory,
            JsonFactory $resultJsonFactory       
           ) {
            $this->_resultPageFactory = $resultPageFactory;
            $this->_resultJsonFactory = $resultJsonFactory;
            parent::__construct($context, $attachmentFactory, $coreRegistry);
        }
5
  • try this code, Clear var/generation folder and comment if still have a problem. Commented Feb 27, 2017 at 6:30
  • I run compile command after update code. I got these errors. Edit.php => Incompatible argument type: Required type: \Magento\Backend\App\Action\Context. Actual type: \Vendor\Module\Model\AttachmentFactory; Attachment.php => Extra parameters passed to parent construct: $attachmentFactory, $coreRegistry.
    – Prince
    Commented Feb 27, 2017 at 10:19
  • have you deleted var/generation folder? Commented Feb 27, 2017 at 10:26
  • Yes....clear cache and var/ folder. Still these errors are occur.
    – Prince
    Commented Feb 27, 2017 at 11:21
  • (If you copied the code and didn't correct it)There were some minor errors, I have corrected the code, Please use this code and check now. Commented Feb 27, 2017 at 11:27

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.