2012-01-09 31 views
0

親の$ itemを変更して、親に残りのプロセスを実行させたいと思います。実行する方法? (私はソフトウェア更新によって頻繁に更新される親クラスを変更することはできません。また注意してください、$項目は、それがparent->変数関数内の変数ではありませんされて)親関数内で変数を変更する方法は?

Class Parent{ 
    function __constructor(){....} 
    function save(){ 
     $items = get_items($id, $this->var); 
     // process the data and save 
    } 
} 
Class Child extends Parent{ 
    function __constructor(){ 
     parent::__construct(); 
    } 
    function save(){ 
    $items = get_items($id, $var)// if.. then $var=$this->var, else $var=$something_else 
     // precess data and save 
    } 
} 
+1

のためにこれは、サードパーティの親クラスの一部に悪いデザインのように思えます。いずれのソリューションも、コードの重複またはハックのある回避策のいずれかを意味することになります。どちらも、アップグレード時にメンテナンスの問題があります。これは何のパッケージですか? – cmbuckley

答えて

0

をパラメータとして渡します。例

Class Parent{ 
    function __constructor(){....} 
    function save(){ 
    function save($items = null){ 
     $items = is_null($items) ? get_items($id, $var) : $items; 
     // process the data and save 
    } 
} 
Class Child extends Parent{ 
    function __constructor(){ 
     parent::__construct(); 
    } 
    function save($items = null){ 
     $items = is_null($items) ? get_items($id, $var) : $items; 
     // precess data and save 
    } 
    return parent::save($items); 
} 
+0

私は親クラスを変更することはできません。 – Jenny

0
Class Parent{ 
    protected $items; 
    function __constructor(){....} 
    function save(){ 
     $this->items = get_items($id, $this->var); 
     // process the data and save 
    } 
} 
Class Child extends Parent{ 
    function __constructor(){ 
     parent::__construct(); 
    } 
    function save(){ 
     $this->items = something_else(); 
     parent::save(); 
    } 

}

+0

デザインによっては、子プロセスに保存された変更が上書きされることがあります。 – KingCrunch

+0

$ itemは親のメンバーvarではなく、親関数の中ではvarです。だから、子供の場合、$ itemは同じvarとして認識されないかもしれません – Jenny

関連する問題