2

I have created one interface and declared methods and in my model class i have implemented interface and methods have been defined.

Now in my other constructor class i'm injecting this interface and invoking its methods then its giving error as cannot instantiate interface .... However if i use my model class in the constructor which is implemented interface methods then its working fine.

But i have noticed that customer interface can inject in any class constructor and able to call its methods.

So please help me on how to inject interface in any class constructor and calling its methods.

4

To instantiate an interface Magento2 looks for the preference record in di.xml, so you should declare your preference to tell Magento2 which concrete class to use, like in this example:

<preference for="Magento\Customer\Api\AddressRepositoryInterface"
                type="Magento\Customer\Model\ResourceModel\AddressRepository" />
| improve this answer | |

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.