php - Codeigniter 模型只是实用程序类?

标签 php model-view-controller codeigniter model

在我习惯的 MVC 中,模型类(通常)表示表,这些类的对象是行/域对象。我不明白在 CodeIgniter 中为什么模型类看起来只是单例实用程序类。感觉写错了

$data = array('text' => 'hello');
$this->commentModel->insert($data);

代替

$comment = new Comment();
$comment->text = 'hello';
$comment->save();

有人可以解释为什么 CodeIgniter 以这种方式建模并让我感觉更好吗? (或者告诉我如何修复它。)

最佳答案

CodeIgniter 中的模型是使用单例模式设计的,您是对的。虽然这让许多习惯于使用更多 PHP OOP 方法的人感到困惑,但有几个原因。

The first most simple is that you can load a model just the once and have it available in the super-global for use throughout the system.

这是唯一真正的优点,其余的都是抱歉的解释。

CI was built in 2006 with PHP 4 support as a main priority.

现在 EllisLab 已经从 CI 2.0 中删除了对 PHP 4 的支持,这才刚刚开始改变,但就目前而言,这就是框架的工作方式。

您当然可以加载一个模型,然后为您的模型使用您喜欢的任何 PHP 5 语法。

$this->load->model('comment');

$comment = new Comment(); $comment->text = 'hello'; $comment->save();

关于php - Codeigniter 模型只是实用程序类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4463062/

相关文章:

javascript - 强制下载文件而不是在浏览器中显示

mysql - 应根据下拉列表显示值

ruby-on-rails - 适用于小型开发人员和设计人员团队的 Git 工作流程

java - 将应用程序转换为 MVC 架构

php - 模型中的错误处理 (MVC)

php - Grocery Crud - 在表单提交上打印 post_array

php - selected = 在 Codeigniter PHP 中选择

php - WordPress - 使用原始图像,未裁剪

javascript - 从 PHP 的回显 json 编码数组中迭代 javascript 对象

php - 对象属性名中的特殊字符