dro60731 2010-12-16 16:34
浏览 54
已采纳

Codeigniter模型只是实用程序类?

In the MVC that I'm accustomed to, model classes (usually) represent tables and objects of these classes are rows/domain objects. I don't understand in CodeIgniter why model classes appear to just be singleton utility classes. It feels wrong writing

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

instead of

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

Can someone explain why CodeIgniter does models this way and make me feel better about it? (Or tell me what I can do to fix it.)

  • 写回答

3条回答 默认 最新

  • dragon8997474 2010-12-17 13:10
    关注

    Models in CodeIgniter are designed using singleton pattern you're right. While this seems confusing to many people who are used to working with a more PHP OOP approach, there are a few reasons.

    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.

    That's the only real plus here, the rest are apologetic explanations.

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

    This is only just starting to change now EllisLab has dropped PHP 4 support from CI 2.0, but for now, that's how the framework works.

    You can of course load a model then use whatever PHP 5 syntax you like for your models.

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

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?