Event Listener Hints

Chris D

XenForo developer
Staff member
So now when we create our event listeners, e.g. load_class_controller, we can specify an event hint. In the case of that event, the hint is the class name being called, e.g. XenForo_ControllerPublic_Thread.

This clearly has benefit if you've created one event listener to extend one controller. But what about if you're extending multiple controllers?

Before XenForo 1.2 I'd simply have one code event listener per code event. So if I were extending 5 controllers I'd do so in a single code event listener method via switch / case statements.

Now we have event hints. What's the best thing to do?

One code event listener per controller and make use of event hints?
One code event listener for all my controllers and not use event hints?
 
Last edited:
Just don't set the hint.

Edit: oh I see some other stuff in there, just set the hint to "_" to work on everything I think? This whole hint stuff seems a bit pointless to me, the performance increases would be so minimal from loading a single class into memory which usually just has static methods.
 
Can't you leave it blank and be fine? I haven't upgraded from 1.2 Beta 3 yet so I might be wrong but I've been leaving it blank...
 
Can't you leave it blank and be fine? I haven't upgraded from 1.2 Beta 3 yet so I might be wrong but I've been leaving it blank...

You can. But it's a nice new feature helping to improve performance by avoiding to load your addon classes and methods for each and every controller, datawriter, etc. In particular if you have a lot of addons, this has the potential to make quite a difference. I am currently modifying my own addons to make use of the hinting feature-

For example, as long as only one hint is supported, I do it like this:

PHP:
<?php
class MobileRead_HidePollResults_Listener_Proxy
{
   public static function loadControllerPublicForum($class, array &$extend)
   {
      if ($class == 'XenForo_ControllerPublic_Forum')
      {
         $extend[] = 'MobileRead_HidePollResults_ControllerPublic_Forum';
      }
   }

   public static function loadControllerPublicThread($class, array &$extend)
   {
      if ($class == 'XenForo_ControllerPublic_Thread')
      {
         $extend[] = 'MobileRead_HidePollResults_ControllerPublic_Thread';
      }
   }
}

Then I enter two code event listeners in the xF admin panel, both listening to load_class_controller, only with different event hints, and with the respective callback method from above.

In pre-1.2, I would only use one event listener and my class would look like this:
PHP:
<?php
   /* pre-1.2 */
   public static function loadController($class, array &$extend)
   {
      switch ($class)
      {
         case 'XenForo_ControllerPublic_Forum':
            $extend[] = 'MobileRead_HidePollResults_ControllerPublic_Forum';
            break;

         case 'XenForo_ControllerPublic_Thread':
            $extend[] = 'MobileRead_HidePollResults_ControllerPublic_Thread';
            break;
      }
   }
?>
 
PHP:
   public static function loadControllerPublicThread($class, array &$extend)
   {
      if ($class == 'XenForo_ControllerPublic_Thread')
      {
         $extend[] = 'MobileRead_HidePollResults_ControllerPublic_Thread';
      }
   }
}
Of course you don't need to even wrap your $class in a conditional anymore...

PHP:
<?php



class YourAddOn_Listener

{

public static function extendThreadController($class, array &$extend)

{

$extend[] = 'YourAddOn_ControllerPublic_Thread';

}

}
 
That is true. Currently I'm working on 1.2 exclusive stuff so BC isn't an issue. But definitely a consideration for 1.1.x compatibility.
 
@Chris D would you please explain what the "Event Listener Hints" are used for and how to use them?
It is a significant performance improvement on larger and more active forums, where your event listener is only called for the classes required.

Otherwise 'load_class' without a hint is called on every class used by XenForo which is extendable. This is an extra function call, and conditional to be parsed.

Rather than "load_class_xxx" I just use "load_class" and a hint, and then in the method I prefix a string which maps to a standard structure for how I override stuff.

For example, one of my listeners looks like:
Code:
class SV_WarningImprovements_Listener
{
    const AddonNameSpace = 'SV_WarningImprovements';

    public static function load_class($class, array &$extend)
    {
        $extend[] = self::AddonNameSpace.'_'.$class;
    }
}
The AddonNameSpace is then sometimes used in the installer bits as required.

This does mean that I replicate the XenForo code layout under the addon's library folder, which makes matching the XenForo Code and my extension code quite easy.
 
It is a significant performance improvement on larger and more active forums, where your event listener is only called for the classes required.

Otherwise 'load_class' without a hint is called on every class used by XenForo which is extendable. This is an extra function call, and conditional to be parsed.

Rather than "load_class_xxx" I just use "load_class" and a hint, and then in the method I prefix a string which maps to a standard structure for how I override stuff.

For example, one of my listeners looks like:
Code:
class SV_WarningImprovements_Listener
{
    const AddonNameSpace = 'SV_WarningImprovements';

    public static function load_class($class, array &$extend)
    {
        $extend[] = self::AddonNameSpace.'_'.$class;
    }
}
The AddonNameSpace is then sometimes used in the installer bits as required.

This does mean that I replicate the XenForo code layout under the addon's library folder, which makes matching the XenForo Code and my extension code quite easy.

In my case, I am already using load_class_xxx. Does that mean that I do not need to use the "Event Listener Hints"?
 
Top Bottom