0

これは継承に関する質問です...主に。初心者はこちら!

私が持っているもの: 親モデルの 2 つの子モデル。この質問の目的では、2 つの子モデルはまったく同じです。

私が欲しいもの:子モデルに親モデルのプロパティを設定させ、そのプロパティをすべての子に設定させます。もし可能なら。たとえば、Info_model は継承されたプロパティ $this->league_id を設定し、League_model は Info_model によって設定されたこの値にアクセスできます。Ml_league_model が読み込まれるまで、league_id の値はわかりません。これにはどのような回避策がありますか?

私の考え: Info_model と League_model が読み込まれると、子がプロパティにアクセスできる 1 つのインスタンスである必要があるのではなく、それぞれが親モデルのインスタンスを持っているようです。

私のコントローラーでは、ご覧のとおり、 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