3

I am looking at the official docs here

Why is it that when changing the preference of a class marked with @api you can depend on a MAJOR version but when you change the preference of an interface marked with @api you have to depend on a MINOR version release. Is it not kinda the same thing?

Same thing goes for implementing an interface (depend on MINOR) and extending a concrete class (depend on MAJOR) both marked with @api

What is the reasoning behind this?

IMHO implementing an interface should not make me more dependent on a module than extending a concrete class.

3
  • It feels weird to me too... shouldn't it be other way around? – Vivek Kumar Jul 8 '19 at 6:25
  • I think they should have the same dependency level, whichever might that be @VivekKumar – vitoriodachef Jul 8 '19 at 6:56
  • In my opinion, the interface is being used as a service contract so it must not be changed that often and hence should be MAJOR but the concrete implementation may change to add/improve a feature so that can be a MINOR. – Vivek Kumar Jul 8 '19 at 7:39
1
+100

I also read the same technical documentation while ago and came to the same conclusion. On the surface it doesn't make sense.

A year or so later and I'm still confused by it.

My best guess is someone at Magento was trying to preemptively predict that certain areas were going to undergo a significant change.

Personally I've rarely coded something that depends on a certain version. And if I face incompatibility I create a different version of the file(s) to work with that particular version. Model21.php, Model22.php, Model23.php etc Wyomind take similar approach. It's a bit untidy but gets the job done. Plus your code is still readable. Just means you need someone with a bit of knowledge to install extension especially if you bundle all the files together.

But I'll admit I haven't produced anything too revolutionary. I generally try to work within the realms of the framework.

6
  • Hi Dominic. Thank you for your answer. The trouble is they ask this kind of questions in the Certification exam. Is harder to remember smth that does not make sense. – vitoriodachef Jul 3 '19 at 4:14
  • are you preparing for developer plus certification? @vitoriodachef – aravind Jul 5 '19 at 13:11
  • No. But I have plenty of experience with these type exams. Before I became a web developer at an agency I worked an an IT trainer and taught CIW ciwcertified.com plus did the odd bit of freelance. Although the topics change the methods of testing and creating a certification / exam are the same. – Dominic Xigen Jul 5 '19 at 19:52
  • @aravind Just developer – vitoriodachef Jul 7 '19 at 11:26
  • I am also preparing for the same..@vitoriodachef – aravind Jul 8 '19 at 4:36
0

I'm leaving here a possible answer, more of an assumption.

Regarding the preference issue the assumption is that the rewrite class is extending the original class, so if a method is added to the original class the rewrite will inherit it. As for the preference of an interface, if a method is added to the interface, the code will break.

To the other one implementing and interface vs extending a class the same principle applies, if a method is added to the interface the code will break, but if adding a method is added to the parent class the method will be inherited and the code will not break.

This however does not apply to case in which a method is added to the concrete class and there already exists such a method in the child class with a different signature. Or if in the parent class an abstract method is added.

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.