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

Extending Model

Discussion in 'XenForo Development Discussions' started by Daniel Hood, Aug 29, 2013.

  1. Daniel Hood

    Daniel Hood Well-Known Member

    I'm about make an addon that makes use of a new function I'm going to add to the User Model. I don't imagine anyone else using it. I know the common practice is to extend using the proxy system, but I don't think it's necessary to load my class every time the user model is called upon, so I'm considering using

     class My_Model_User extends XenForo_Model_User
    and then calling
     $model XenForo_Model::create('My_Model_User'); 
    instead of calling the XenForo Model. I just want know if people are going to view this as bad practice? I'm basically wanting to not sacrifice performance where it's not really needed (which is anywhere besides my addon).
  2. Chris D

    Chris D XenForo Developer Staff Member

    I'd view this as pretty bad practice.

    Just extend the model with the class proxy system. That's what the code events are for, use them always where it is possible to do so.

    To reduce the impact of loading your class, when you create the event listener, use XenForo_Model_User as the event hint and that's the best you can do.

    Bear in mind until 1.2 we didn't have event hints at all, so even if you do it the proper way, overall the performance is better than it was in 1.1.x anyway.
    Daniel Hood likes this.
  3. Jeremy

    Jeremy XenForo Moderator Staff Member

    Never extend a XenForo class directly. You should always use the Class Proxy system. You may not think its necessary but the extension is not much of a memory/time hit and it guarantees proper functioning of other add-ons that may in turn end up working with yours.
    risjarv, Daniel Hood and Chris D like this.
  4. Daniel Hood

    Daniel Hood Well-Known Member

    Fair enough, I was on the fence as I knew it was wrong to ignore the code event system but I figured what's the point of my file being included for no reason, no big deal either way. My function (or two) won't cause too much overhead regardless.

    Thanks for your feedback
    Chris D likes this.

Share This Page