2011-12-13 18:32
浏览 41


The way I create CodeIgniter models at the moment is (i.e. no constructor, having to pass userID all the time and limited to one object):

$this->user->set_password($userID, $password);

But I would like to do it like this:

$User = new User($userID);

UPDATE: Perhaps just a user model was a poor example.

For instance, if I have a shopping list that has various items I would like to use PHP in this way:


$List = new List();
$items[] = new Item($itemName1, $itemPrice1);
$items[] = new Item($itemName2, $itemPrice2);


CodeIgniter feels fundamentally broken in handling PHP OO in this way. Does anyone have any solutions that can still use the superobject within every model?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douju7765 2011-12-13 18:43

    You can do it like you normally would:

    $User = new User($userID);

    Or, you can rely on a table-level model to return a record-level model:

    $this->load->model('users_model'); // users (plural) is the table-level model
    $User = $this->users_model->get_user($userID);

    Meanwhile, in users_model

    public function get_user($userID)
      // get a record from the db
      // map record to model
      // return model
    解决 无用
    打赏 举报
  • dongshetao1814 2011-12-13 18:46

    you can subclass the model or just add something in the constructor I think you need to adjust code to do it the way you're looking to…

    解决 无用
    打赏 举报

相关推荐 更多相似问题