0

这是一个继承问题……主要是。新手来了!

我有:一个父模型的 2 个子模型。对于这个问题,两个子模型完全相同。

我想要什么:让子模型设置父模型的属性并为所有子模型设置该属性。如果可能的话。例如,Info_model 设置继承的属性 $this->league_id 然后 League_model 可以访问由 Info_model 设置的这个值。在加载 Ml_league_model 之前,我不知道league_id 的值是多少。这有什么解决办法?

我的想法:似乎在加载 Info_model 和 League_model 时,它们每个都有一个父模型的实例,而不是我想要的,这是一个孩子可以从中访问属性的实例。

如您所见,在我的控制器中,test() 函数的输出为:

league id in the info_model is: 508
league id in the league_model is: 

父模型位于 /core 目录

class Ml_league_model extends MY_Model{

    protected $league_id='';

    function  __construct() {
        parent::__construct();
    }

function _initialize($params = array())
{
    if (count($params) > 0)
    {
        foreach ($params as $key => $val)
        {
            $this->$key = $val;
        }
    }
}//end initialize
}
?>

位于 /models 目录中的子模型

class Info_model extends Ml_league_model{

function  __construct() {
    parent::__construct();
}

function get_league_id(){
    echo 'league id in the info_model is: '.$this->league_id.br(1);
}

位于 /models 目录中的另一个子模型

class League_model extends Ml_league_model{



function  __construct() {
    parent::__construct();
}

function get_league_id(){
    echo 'league id in the info_model is: '.$this->league_id.br(1);
}

位于 /controllers 目录的控制器

Class Info extends MY_Controller{


public function __construct() {
    parent::__construct();

    $this->load->model('Info_model');
    $this->load->model('League_model');

}

function test (){

    $this->Info_model->_initialize(array('league_id'=>508));
    $this->Info_model->get_league_id();
                $this->League_model->get_league_id();

}
4

0 回答 0