• 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?

Jaxel

Well-known member
#1
Okay... lets say I have:
Code:
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?
 

g0rn

Well-known member
#2
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.
 

Kier

XenForo Developer
Staff member
#5
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.
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
PHP:
$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');
}
 
#6
If you don't want to instantiate your model and run it's constructor/deconstructor you can do this:

PHP:
$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');
}
 
#7
Yes, class_exists should work as well and it will finally call XenForo_Application::autoload. So why not to call this directly?
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.
 

Kier

XenForo Developer
Staff member
#8
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.
 

Jaxel

Well-known member
#9
Okay... continuing on this point... this will check if a model exists...
Code:
if (XenForo_Application::autoload('EWRmedio_Model_Premium'))
How would I check if the model has a function called "getModule" or "getBypass"?
 

Shadab

Well-known member
#10
Expanding upon what Darren has posted above...
PHP:
    // check if the getModule() method is defined
    $hasGetModule = $reflection->hasMethod('getModule');