PHP继承

发布于 2024-11-19 16:57:39 字数 560 浏览 0 评论 0原文

我有以下 3 个类:

class ParentClass {
    protected $myVar = array();

    function __construct() {
        var_dump($this->myVar);
    }
}

class FirstClass extends ParentClass {
    protected $myVar = array('defaultVal');
}

class SecondClass extends FirstClass {
    protected $myVar = array('anotherVal');
}

如果我要执行以下操作:

$class = new SecondClass();

那么我会得到 array('anotherVal') 我可以将什么添加到 ParentClass 的构造中,以便我实际上得到 array('defaultVal', 'anotherVal')

提前致谢!

I have the following 3 classes :

class ParentClass {
    protected $myVar = array();

    function __construct() {
        var_dump($this->myVar);
    }
}

class FirstClass extends ParentClass {
    protected $myVar = array('defaultVal');
}

class SecondClass extends FirstClass {
    protected $myVar = array('anotherVal');
}

If I was to do the following:

$class = new SecondClass();

Then I would get array('anotherVal') what can I add to the construct of ParentClass so that I would actually get array('defaultVal', 'anotherVal')

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

深者入戏 2024-11-26 16:57:40

使用此页面中的想法,我建议如下

class ParentClass {
    protected $myVar = array();

    function __construct() {
        var_dump($this->myVar);
    }
}

class FirstClass extends ParentClass {
    function __construct() {
        array_unshift($this->myVar, "defaultVar");
        parent::__construct();
    }
}

class SecondClass extends FirstClass {
    function __construct() {
        array_unshift($this->myVar, "anotherVal");
        parent::__construct();
    }
}
$class = new ParentClass();
$class1 = new FirstClass();
$class2 = new SecondClass();

:以下输出:

array(0) {
}
array(1) {
  [0]=>
  string(10) "defaultVar"
}
array(2) {
  [0]=>
  string(10) "defaultVar"
  [1]=>
  string(10) "anotherVal"
}

Using ideas from this page, I would suggest the following:

class ParentClass {
    protected $myVar = array();

    function __construct() {
        var_dump($this->myVar);
    }
}

class FirstClass extends ParentClass {
    function __construct() {
        array_unshift($this->myVar, "defaultVar");
        parent::__construct();
    }
}

class SecondClass extends FirstClass {
    function __construct() {
        array_unshift($this->myVar, "anotherVal");
        parent::__construct();
    }
}
$class = new ParentClass();
$class1 = new FirstClass();
$class2 = new SecondClass();

Which gives me the following output:

array(0) {
}
array(1) {
  [0]=>
  string(10) "defaultVar"
}
array(2) {
  [0]=>
  string(10) "defaultVar"
  [1]=>
  string(10) "anotherVal"
}
绝對不後悔。 2024-11-26 16:57:40

经过进一步研究,我找到了一种简单的方法get_class_vars

class ParentClass {
    protected $myVar = array();

    function __construct() {
        $firstClassVars = get_class_vars('FirstClass');
        $this->myVar= array_merge($this->myVar, $firstClassVars['myVar']);
        var_dump($this->myVar);
    }
}

这解决了我的问题...

无论如何谢谢大家!

After furthur research i found out an easy way to do it get_class_vars:

class ParentClass {
    protected $myVar = array();

    function __construct() {
        $firstClassVars = get_class_vars('FirstClass');
        $this->myVar= array_merge($this->myVar, $firstClassVars['myVar']);
        var_dump($this->myVar);
    }
}

This solves my problems...

Thanks anyway guys!

始于初秋 2024-11-26 16:57:39

出于某种奇怪的原因,堆栈溢出不断删除我之前的答案。这是新的。

class ParentClass {
    protected $myVar;

    function __construct() {
        $this->myVar = array();
    }
}

class FirstClass extends ParentClass {
    function __construct() {
        parent::__construct();
        $this->myVar[] = 'default val';
    }
}

class SecondClass extends FirstClass {
    function __construct() {
        parent::__construct();
        $this->myVar[] = 'another val';
    }

    public function printArr() {
        print_r($this->myVar);
    }
}

$class = new Secondclass();
$class->printArr();

For some odd reason, stack overflow keeps erasing my previous answer. So here is the new one.

class ParentClass {
    protected $myVar;

    function __construct() {
        $this->myVar = array();
    }
}

class FirstClass extends ParentClass {
    function __construct() {
        parent::__construct();
        $this->myVar[] = 'default val';
    }
}

class SecondClass extends FirstClass {
    function __construct() {
        parent::__construct();
        $this->myVar[] = 'another val';
    }

    public function printArr() {
        print_r($this->myVar);
    }
}

$class = new Secondclass();
$class->printArr();
手长情犹 2024-11-26 16:57:39

将您的分配更改为 array_push()

Change your assignments to array_push().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文