duan19913 2015-03-28 11:02
浏览 34
已采纳

在单个模型中对方法进行分组的正确方法是什么?

I'm sorry for the title, maybe I couldn't describe it well. But here's what I want:

Now if I create a model m.php and put it inside the models folder with the following code:

class m extends CI_Model {

  public function username() {
    print "john doe";
  }

  public function password() {
    print "ABC123";
  }

}

Now, I can simply do the following in the controller :

$this->load->model("m");
$this->m->username(); // prints john doe
$this->m->password(); // prints ABC123

Now what I need to do is to make it in a way on which I can call the methds above as following:

$this->m->genralinfo->username(); // prints john doe

$this->m->privacy->password(); // prints "ABC123"

How can I achieve this ? I want to do this in order to group the methods that are specific to something alltogether, this helps me to structure my code better.

**I am aware that it is possible to create a new model for every group of methods but all I'd like to keep everything that is related to the user inside a single model **

Is this even possible using a single model ?

  • 写回答

3条回答 默认 最新

  • douda5706 2015-03-28 13:55
    关注

    I think you want like this

    class m extends CI_Model
    {
        public $genralinfo;
        function __construct()
        {
            parent::__construct();
            $this->genralinfo=new Genralinfo();
        }
    
    }
    class Genralinfo
    //class Genralinfo extends CI_Model//you may extend CI_MODEL too
    {
        public function username() {
            print "john doe";
        }
    
        public function password() {
            print "ABC123";
        }
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿