Jump to content

Best way to override a class (which is already overridden)


fedesib

Recommended Posts

Hi everybody,

 

for a client's project I need to override a class, but this class has already been overridden by another module.

 

The "dirty" solution is of course to modify the overridden class, but this can lead to some issues if the module has to be updated (i.e. the modification should be manually replaced).

 

Is there a better/smarter way to do this?

 

I'm working on PS 1.6.1.17

 

Thank you for your time,

Federica

Edited by fedesib (see edit history)
Link to comment
Share on other sites

Sometimes it is possible to use a hook instead of overriding a class, but not always. If possible I would choose this way. If not, there is no other way than joining both overrides into one file. Downside as you already said: You will loose the upgradeability with that approach.

Link to comment
Share on other sites

Hi Scully,

 

thanks a lot for your answer. Unfortunately in my case, there's no hook to be used, thus I will go on with merging my override with that already done by the module.

I hope that in the future the core developers of Prestashop will find a suitable way to overcome this!

 

Thanks and have a nice day,

Federica

Link to comment
Share on other sites

You're welcome. I don't see a chance for this to be changed. Why not?

 

Functional perspective

Two overrides for the same object could always act against the other. An example: You have an override which adds some kind of additional payment fees and another override which does the opposite. No chance to merge them automatically.

 

Technical perspective

If an override uses an existing function name, the same function name cannot be used twice.

 

Best practice as a conclusion:

Only override what needs to be overriden. It makes much sense not to copy / paste whole files but only functions which are changed in the override. This way we have to advantages.

 

1) Code maintainability and readability is much better.

2) Most of the code is still using the core functionality.

  • Like 1
Link to comment
Share on other sites

Hi Scully,

 

thanks for your explanation: very clear.

You are right under many points of view and the way overrides work right now is just the more logical as you pointed out.

I was just dreaming of a method for merging different overrides so that it could be easier for a developer to upgrade and maintain her own modification to the code. Something like an override "order" that you can use to say "hey, this is an override of this override". Just a dream.

 

Best,

Federica

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...