1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Extending a class, among other things

Discussion in 'XenForo Development Discussions' started by Valhalla, Mar 25, 2014.

  1. Valhalla

    Valhalla Well-Known Member

    If you extend a class and create a method with an identical name, which is the outcome?

    Given the above, how can I extend a class on one particular route only? I mean: would doing that be considered normal practice? Does wanting to do that indicate I'm doing something wrong if I'm having conflicts?

    I'm having conflicts, but that's only because I'm trying to use the already-existing methods where available because they're mostly useful (except when they're not).

    Also, how can I ensure there are no conflicts (more generally) with my addon against any other ones?

    EDIT: I do think I've approached this the wrong way. Instead of extending a class, I can simply create a new one (like, duplicate), and 'discard' any residual, unrequired methods at the end.
    Last edited: Mar 26, 2014
  2. Adrian Schneider

    Adrian Schneider Active Member

    The answer will depend on what you're trying to extend.

    If you want to add new controller actions (loosely maps to new URLs you can hit), then it'll depend on how much you want to leverage other functionality in that controller. Adding a new method is easier than a whole new class, with new routes, etc. If you are adding substancial amounts of code, a new class is probably easier to maintain.

    If you are extending other classes, you'll have to ask yourself whether the new functionality belongs in that class or not. In many cases, it will have to be in the same.

    If multiple classes override the same method, it will depend on the plugin execution order to depend which one ends up happening. However, most issues can be mitigated by properly calling the parent::methodName() in that overridden method. It just takes one modification to ruin everything by not doing this.

    Now, if you extend a class and add a new method, there is and will always be room for conflict there. It's probably best to be specific enough with your naming that conflicts don't happen. When they do happen, it's going to be very hard to track down because it's not guaranteed to spit out any errors.
    AndyB and Valhalla like this.
  3. Valhalla

    Valhalla Well-Known Member

    Thanks for the reply - really helpful.

Share This Page