yii2-activerecord-inheritance icon indicating copy to clipboard operation
yii2-activerecord-inheritance copied to clipboard

Was not overridden parent attributes while calling parent method

Open inotloh opened this issue 6 years ago • 0 comments

class Parent extends ActiveRecord
{    
    $test = '123';

    public function text()
    {
        echo $this->test;
    }
}


class Child extends ActiveRecord implements ActiveRecordInheritanceInterface
{
    use ActiveRecordInheritanceTrait;
    
    $test = '456';

    public static function extendsFrom()
    {
        return Issue::className();
    }
}

$child = new Child();
$child->text();
// must be echo '456' but I got '123'

I was write solution, but it's bad solution. Put this into Child class.

    public function __call($name, $params) {
        try {
            return parent::__call($name, $params);
        } catch (UnknownMethodException $e) {
            foreach (get_object_vars($this) as $cur_obj_attr_name => $cur_obj_val) {
                foreach (get_object_vars($this->_parent()) as $parent_obj_attr_name => $parent_obj_val) {
                    if ($cur_obj_attr_name == $parent_obj_attr_name) {
                            $this->_parent()->$cur_obj_attr_name = $cur_obj_val;
                    }
                }
            }
            return call_user_func_array([$this->_parent(), $name], $params);
        }
    }

inotloh avatar Mar 09 '18 11:03 inotloh