Check If Model Exists and is Valid?

Jaxel

Well-known member
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?
 
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.
 
Can someone tell me why class_exists() is not used to do this? XenForo autoloader registers itself onto autoload stack so....
Yes, class_exists should work as well and it will finally call XenForo_Application::autoload. So why not to call this directly?
 
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');
}
 
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');
}
 
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.
 
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.
 
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"?
 
Expanding upon what Darren has posted above...
PHP:
    // check if the getModule() method is defined
    $hasGetModule = $reflection->hasMethod('getModule');
 
Expanding upon what Darren has posted above...
PHP:
    // check if the getModule() method is defined
    $hasGetModule = $reflection->hasMethod('getModule');
Shadab why are you not working with the xenforo team! lol :)
 
Top Bottom