php - Codeigniter:如何判断模型是否已加载?

标签 php codeigniter class model

是否有原生的 codeigniter 函数可以用来判断某个模型是否已经加载?可以使用 php 的 class_exists() 判断模型是否已加载吗?

最佳答案

我很想扩展 CI_Loader 核心类。 (参见 extending Core Class)

class MY_Loader extends CI_Loader {

    function __construct()
    {
        parent::__construct();
    }

    /**
     * Returns true if the model with the given name is loaded; false otherwise.
     *
     * @param   string  name for the model
     * @return  bool
     */
    public function is_model_loaded($name) 
    {
        return in_array($name, $this->_ci_models, TRUE);
    }
}

您将使用以下内容检查给定模型:

$this->load->is_model_loaded('foobar');

That strategy已被 CI_Loader 类使用。

此解决方案支持 CI 的模型命名功能,其中模型的名称可以与模型类本身不同。 class_exists 解决方案不支持该功能,但如果您不重命名模型,应该可以正常工作。

注意:如果您更改了subclass_prefix 配置,它可能不再是MY_

关于php - Codeigniter:如何判断模型是否已加载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14947220/

相关文章:

php - 在 laravel Controller 中使用 if

php - 如何在 CodeIgniter 中仅从日期时间获取日期?

php - Codeigniter默认时区问题

php - 通过按下链接并转义反斜杠来运行 MySQL 查询

php - Laravel 如何在 Controller 中使用 create 函数添加记录?

php - 如何使用 DateTime::createFromFormat 转换 DateTime 格式 [2016-10-05 11:58:04]

php - 如何访问 CI HMVC 中的库?

java子类有方法返回它的类

php - MySQLI 扩展和原生错误处理

jquery - 使用 jQuery 添加和删除类