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

Check If Model Exists and is Valid?

Discussion in 'XenForo Development Discussions' started by Jaxel, Nov 16, 2010.

  1. Jaxel

    Jaxel Well-Known Member

    Okay... lets say I have:
    class EWRmedio_Model_Premium extends XenForo_Model
    Is there a way I can check to see if the class exists and store it in a value like $isPremium?
  2. g0rn

    g0rn Well-Known Member

    I believe you need something like this:
    $isPremium = XenForo_Application::autoload('EWRmedio_Model_Premium');
    This will load a class if it exists and place true or false to the $isPremium variable if the class was successfully loaded or not respectively.
  3. Vladislav Rastrusny

    Vladislav Rastrusny Active Member

    Can someone tell me why class_exists() is not used to do this? XenForo autoloader registers itself onto autoload stack so....
  4. g0rn

    g0rn Well-Known Member

    Yes, class_exists should work as well and it will finally call XenForo_Application::autoload. So why not to call this directly?
  5. Kier

    Kier XenForo Developer Staff Member

    It will tell you if the class exists and is loadable, but it won't tell you if the class is a model or not.

    For that, you need to do something like
    $isPremium false;

    // check that the class exists and is loadable
    if (XenForo_Application::autoload('EWRmedio_Model_Premium'))
    // attempt to instantiate the model
    $model = @XenForo_Model::create('EWRmedio_Model_Premium');

    // check that the instantiated model is an extended XenForo_Model
    $isPremium is_a($model'XenForo_Model');
    Jeremy likes this.
  6. Darren Gordon

    Darren Gordon Member

    If you don't want to instantiate your model and run it's constructor/deconstructor you can do this:

    $isPremium false;

    // check that the class exists
    if (class_exists('EWRmedio_Model_Premium'))
    // create a reflection class to examine the class
    $reflection = new ReflectionClass('EWRmedio_Model_Premium');

    // check if the class is an extended XenForo_Model
    $isPremium $reflection->isSubclassOf('XenForo_Model');
    Fuhrmann and Kier like this.
  7. Vladislav Rastrusny

    Vladislav Rastrusny Active Member

    I think we need to use PHP features when programming on PHP :) class_exists('EWRmedio_Model_Premium') is shorter than XenForo_Application::autoload('EWRmedio_Model_Premium') and faster to type. Also, if these two really mean the same, it's better to use class_exists() because it makes your code independent on autoloader. I don't see why some classes in the framework should be autoloaded and others loaded manually. It doesn't make sense to me. Manual class loading is almost like require() we want to avoid. We should rely on PHP on loading our classes where needed.
  8. Kier

    Kier XenForo Developer Staff Member

    When you call a class directly in XenForo, it will use the autoloader anyway - we simply catch the 'class not found' exception and attempt to rectify it with the autoloader.
    Vladislav Rastrusny likes this.
  9. Jaxel

    Jaxel Well-Known Member

    Okay... continuing on this point... this will check if a model exists...
    if (XenForo_Application::autoload('EWRmedio_Model_Premium'))
    How would I check if the model has a function called "getModule" or "getBypass"?
  10. Shadab

    Shadab Well-Known Member

    Expanding upon what Darren has posted above...
        // check if the getModule() method is defined
    $hasGetModule $reflection->hasMethod('getModule');
  11. Wuebit

    Wuebit Well-Known Member

    Shadab why are you not working with the xenforo team! lol :)

Share This Page