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

XenForo_Model override issue (perhaps?)

Discussion in 'XenForo Development Discussions' started by LennardF1989, Sep 1, 2014.

  1. LennardF1989

    LennardF1989 Member

    Hello everyone,

    I have been working on an extension for a client of mine and ran across a slight problem, which I managed to solve, but with a workaround.

    Consider the following:
    - XenForo_Model has a method called "prepareStateLimitFromConditions" I want to override.
    - I have a MyExtension_Model_Post and a MyExtension_Model_Thread class, both registered in my listener to extend the originals. They originally already override a lot of methods, except for "prepareStateLimitFromConditions", which is yet to be implemented at this point.

    Since both these class have to use the same "prepareStateLimitFromConditions"-method, I figured I'd just make a MyExtension_Model extending XFCP_MyExtension_Model with said method defined.

    Since it's no use trying to add a listener for XenForo_Model, I figured I just add two extends to XenForo_Model_Post and XenForo_Model_Thread. The first one to MyExtension_Model, the second to either MyExtension_Model_Post or MyExtension_Model_Thread respectively.

    However, this generates an error where it tells me you can't extend from the same class twice.

    Eventually I "solved" it by removing changing MyExtension_Model back to just a class, making the method static, then override method in the two classes twice, just calling the static method.

    I was wondering, where did I go wrong, because I don't like my "hack", and it seems my initial way of solving it should have worked.
  2. LennardF1989

    LennardF1989 Member

    No one knows? Or is my case unclear?
  3. LennardF1989

    LennardF1989 Member

    A little bit more information...
    case 'XenForo_Model_Thread':
    $extend[] = 'MyExtension_Model'//Contains the prepareStateLimitFromConditions override
    $extend[] = 'MyExtension_Model_Thread'//Contains other overrides

    $extend[] = 'MyExtension_Model'//Contains the prepareStateLimitFromConditions override
    $extend[] = 'MyExtension_Model_Post'//Contains other overrides
    My class definitions:
    class MyExtension_Model extends XFCP_MyExtension_Model
    class MyExtension_Model_Thread extends XFCP_MyExtension_Model_Thread
    class MyExtension_Model_Post extends XFCP_MyExtension_Model_Post
    Will throw an error
    Fatal errorCannot redeclare class XFCP_MyExtension_Model in D:\Websites\Dev\library\XenForo\Application.php(520) : eval()'d code on line 1
    How come? It works fine in most other cases.
    Last edited: Sep 9, 2014
  4. Aayush

    Aayush Well-Known Member

    You are loading MyExtension_Model twice in the listener. You should remove it from one of the case.
  5. LennardF1989

    LennardF1989 Member

    If I remove it from one of the cases, one of the two will not have the prepareStateLimitFromConditions overwritten, so that can't be the solution, right?

Share This Page