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

Call to undefined method -- which isn't even on that line

Discussion in 'XenForo Development Discussions' started by LPH, Apr 10, 2016.

  1. LPH

    LPH Well-Known Member

    I have a class, which sits outside of XenForo, and is called to change an author url link in WordPress. Today I decided to work on building the link using XenForo methods rather than manually building. However, I'm getting something REALLY strange.

    Here is the class:

    PHP:
    class Author_Link {

        public function 
    __construct() {
            
    add_filter'author_link', array( $this'xenword_author_link' ), 100 );
        }

        public function 
    xenword_author_link() {

            global 
    $post;

            
    $thread_id get_post_meta$post->ID'thread_id'true );

            
    $threadModel XenForo_Model::create('XenForo_Model_Post');
            
    $thread $threadModel->getThreadById($thread_id);

            
    $xf_author_link XenWord::getBoardUrl() . '/' XenForo_Link::buildPublicLink('members'$thread);
            return 
    $xf_author_link;
        }
    }

    new 
    Author_Link();
    Line 44 is the getThreadById.

    The error being returned is ...

    Code:
    An exception occurred: Call to undefined method Dark_PostRating_Model_Post::getThreadById()
    
    As you can see, Dark_PostRating_Model isn't being called. I'm stumped. Any ideas?
     
  2. LPH

    LPH Well-Known Member

    Oh my. This is embarrassing. The Model is not Post but should be Thread.

    This works.

    PHP:
        public function xenword_author_link() {

            global 
    $post;

            
    $thread_id get_post_meta$post->ID'thread_id'true );
            
    $thread XenForo_Model::create('XenForo_Model_Thread')->getThreadById$thread_id );

            
    $xf_author_link XenWord::getBoardUrl() . '/' XenForo_Link::buildPublicLink('canonical:members'$thread);

            return 
    $xf_author_link;
        }
    Why it didn't tell me in the error that the function didn't exist in XenForo_Model_Post is really wild.
     
    drsprite likes this.
  3. Jeremy P

    Jeremy P Well-Known Member

    It's because you're using another add-on that extends the XenForo_Model_Post class (Post Ratings). That's how the class proxy system works, it chains them together so that they all extend one another until a cumulative class is formed.
     
    Last edited: Apr 10, 2016
  4. katsulynx

    katsulynx Well-Known Member

    By statically instantiating the base classes from XenForo, you'll never load any of those addons that extend them. It would be best if you could implement a function similar to the existing one from XenForo that fetches the respective class from Cache, then you make sure to get all addon extensions, otherwise you might cause some incompatibilities.
     
  5. LPH

    LPH Well-Known Member

    Does it start looking in XenForo_Model_Post then looks at the extensions or vice versa? Just curious now that you've explained this !

    I'm not sure what you mean. I struggle with OOP because WordPress and OOP aren't best friends and flipping between procedural and OOP gets me in trouble at times.

    I have a main class named XenWord. In it is a method that initializes XenForo, etc. Are you suggesting this class extends XFCP_XenForo_ ?
     
  6. katsulynx

    katsulynx Well-Known Member

    Have a look at
    Code:
    XenForo_Controller::getModelFromCache($class)
     
  7. Jeremy P

    Jeremy P Well-Known Member

    @LPH is already using the static create() method to instantiate models. This will include all add-on extensions already. You would be right if he were using this though:
    PHP:
    $model = new \XenForo_Model_Post;

    Since XenForo core models would be the root class, they are the last class "checked." Depending on execution order and the number of add-ons extending a particular class, you get things like:
    • Jrahmy_Cool_Model_Post extends Dark_PostRating_Model_Post
    • Dark_PostRating_Model_Post extends XenForo_Model_Post
     
    Last edited: Apr 10, 2016
    LPH and katsulynx like this.
  8. katsulynx

    katsulynx Well-Known Member

    I totally overlooked that, damn myself. Sorry for the confusion!
     
    Jeremy P likes this.

Share This Page