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

XF 2.0 Advantage of Code event listener over Class Extension for working in classes as _postSave()?

Marcus

Well-known member
#1
From what I see it is very convenient to extend the class for _postsave() as I have access to all objects (and $this) there. I could somehow emulate a code event listener by calling the parent class first and then execute my own things. What is the advantage of using the code event listener? There are some good guidelines from xf team how to program as like here, so it seems the most unobtrusive way to add something in _postSave() would be to extend it, and not listen to the code event.
Mike said:
(...) In this particular example, not using the service would generally be considered wrong. The service is what's responsible for dealing with things like quote and mention alerts and notification. These are not things that should be handled in entities; in general, entities deal more with data validation and other things that technically need to happen for a working system. Sending an alert doesn't fit in that and thus would normally be a level above that. (...)
https://xf2demo.xenforo.com/threads/how-to-use-the-thread-creator-to-create-a-thread.2002/ There are some hidden gems in the xfdemo forum as like @ChrisD one post further down the same discussion advised to always use the entity when you already have the key.
 
Last edited:

Mike

XenForo developer
Staff member
#2
If done properly, it's roughly the same thing, though it depends what you're trying to do. If you're not extending the class otherwise and don't need access to anything that's explicitly protected within the entity, then arguably it'd be more efficient to use a listener than a class extension.

It was also somewhat common in XF1 for people to forget to call the parent version of the method, which has caused quite a few headaches for us. You wouldn't have to worry about that with the event.