在我习惯的 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/