Codeigniter检查库/helper/core文件是否加载的正确方法



我使用codeigniter 2。

如果有人能告诉我检查以下文件的正确方法,我将不胜感激:
-是否加载库文件?
-是否加载helper文件?
-是否加载配置文件?
-是否加载模型文件?
-是否加载third_party文件?

你可以使用本地PHP函数class_exists()来判断这个类是否已经定义,在调用它之前。同样,使用method_exists()将检查类方法是否存在。

由于helper是函数的集合,而不是方法的集合,因此可以使用function_exists()进行检查。

if (class_exists('Library')) 
{
    $this->library->myMethod();
}

更多信息请参考

http://php.net/manual/en/function.class-exists.php。http://us.php.net/manual/en/function.method-exists.php

你不需要检查,只要在你需要的地方加载它们就可以了。

使用CI的加载库($this->load->[library|model|helper])总是只加载一次。如果您打开调试日志,就可以看到这一点。

这是检查已加载库的codeigniter方法。

//If the library is not loaded, Codeigniter will return FALSE
if(!$this->load->is_loaded('session'))
{
     $this->load->library('session');
} 

一旦加载,您的实例就存储在控制器上,因此要检查模型是否已加载:

if (isset($this->my_model))

其中$this为控制器

最好的方法是使用Codeigniter的Loader类。Loader又名load有一个内置方法is_loadedis_loaded方法检查类是否已经加载。

如果类没有被加载,则is_loaded返回FALSE,否则返回属性名。

的例子:

$this->load->library('table');
$this->load->is_loaded('table'); //Returns 'table'
$this->load->is_loaded('blabla_library'); //Returns FALSE

如果您想在加载之前检查库是否存在,您需要遵循以下简单的方法。

希望,它会有所帮助。

if(file_exists(DOC_ROOT."application/front/libraries/" . ucfirst($library).".php"){
$this->load->library($library);}else {
echo "No library found";}

最新更新