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

Have extended class automatically call method of other extended class?

Discussion in 'XenForo Development Discussions' started by AlexT, Apr 1, 2013.

  1. AlexT

    AlexT Well-Known Member

    I have XenForo_BbCode_Formatter_Base extended with my own class to add custom BB Codes (via the getTags() method).

    Now I want other classes that extend XenForo_BbCode_Formatter_Base to inherit my custom getTags() method. Is that even possible?

    Here is why: Take the XenForo_BbCode_Formatter_BbCode_AutoLink class for example. It extends the XenForo_BbCode_Formatter_Base and refers to it in its constructor to get the list of BB Codes. Unfortunately, that list won't include my custom BB Codes since the call goes directly to XenForo_BbCode_Formatter_Base and skips my class.

    I am still learning about the concepts and possibilities of class hierarchies, so I guess my question is, can I somehow control that calls from extended classes to their parents will first go to my class which is an extended class of the parents? Or would I have to go through the pain and extend all methods from the classes that extend XenForo_BbCode_Formatter_Base and that should be aware of my custom BB Codes?
  2. Arty

    Arty Well-Known Member

    You can use "parent" keyword to call parent class's function:
    No, you don't need to extend all methods. That's what class hierarchy is for - if you don't overwrite a method in your class, parent class's method will be used.
  3. AlexT

    AlexT Well-Known Member

    My fault, I think I didn't express myself properly. What I was referring to is multiple inheritance, of lack thereof. Because PHP doesn't support it, this can cause problems with add-ons that are extending the same base class. Basically, if I have two add-ons extending the same base class (XenForo_BbCode_Formatter_Base from the example above), neither of the add-ons will be aware of the other add-on's extended class. That is a problem, if, let's say, I want an add-on to change forum functionality on a global level.

    From the example above, let's say I have add-on One that adds custom BB codes via extending XenForo_BbCode_Formatter_Base. Now, we have add-on Two that does something else and to so needs to get all available BB code tags; it does so too by extending the XenForo_BbCode_Formatter_Base class. Unfortunately, it will only know of the standard BB codes delivered by the base class since the extended class from add-on One is never called. :(
  4. Jeremy

    Jeremy Well-Known Member

    You should never extend XenForo_BbCode_Formatter_Base class directly. Always extend using the code events and XFPC_YOURCLASSNAME. XenForo will create multi-inheritance depending on execution order (you can define how early or late your code event listener is run when adding it).

    Word of warning: Always call parent::functionName() so you don't break other add-ons.
    AlexT likes this.
  5. AlexT

    AlexT Well-Known Member

    Thanks for your help, King Kovifor.

Share This Page