Not planned Adding the «extend_class» event

NikitOS

Well-known member
Based on the thread «Unable to add new field type for custom fields».

The only idea I got was to add an extension to the class that is a child of the abstract I needed, on the fly. However, this is not possible, since the class loading event that XF 1 had is not.
That is, I imagined an event listener like this:
PHP:
public static function extendClass($class, &$extensions)
{
    if (is_subclass_of($class, 'Needle\AbstractClass'))
    {
        $extensions[] = 'My\Extension\Class';
    }
}
 
Upvote 2
This suggestion has been closed. Votes are no longer accepted.
We won't be implementing a load_class event system like XF1. It was replaced by the class extension system.

The bug you reported earlier - if it is deemed to be fixed - may be fixed with a specific code event for that purpose and that's an approach we'd prefer for other places too.
 
The bug you reported earlier - if it is deemed to be fixed - may be fixed with a specific code event for that purpose and that's an approach we'd prefer for other places too.
It can be fixed with a specific code event, but it still hasn't been fixed…
 
I'm not sure what you mean.

I haven't looked too deeply into it, yet, but theoretically I think a new code event will fix the issue you reported. It hasn't been fixed yet because you only reported it a few hours ago and it's a weekend...
 
Yes, I reported it a few hours ago, but there is a suggestion (or half a bug report) published two years ago. Maybe it was missed. :)

In any case, I hope that someday events will be added, because there are no adequate solutions right now.
 
Such a code event wouldn't help (much) to extend (abstract) base classes as those can't be extended via class proxy - they are hardcoded references.

To make (abstract) base classes extendable via class proxy, every class extending the base class would instead have to extend a non-existing virtual class which would complicate things and could cause performance issues.

That being said, I think there is a way to add a new custom field type and have this supported everywhere (a bit complicated, but doable):
  1. Add code event listeners for addon_post_* and scan the Add-On PHP files to find classes deriving from the class(es) you need to extend
  2. Autogenerate specific extension class PHP code for those classes based on a template, probably using a trait
  3. Keep a list of those extensions somewhere (simpleCache maybe)
  4. Add a code event to app_setup to add your extensions from the list via \XF\Extension::addClassExtension()
As an alternative for steps 3+ you could also just auto-generate "normal" class extensions, but you would have to deal with those upon upgrades of your Add-on.

This way you still won't be able to extend the (abstract) base classes, but the specific child classes without exactly knowing them beforehand.
This approach should work for many extendable classes.
 
Last edited:
Back
Top Bottom