如何强制父类保留其受保护属性的值


How to force parent class to keep value of its protected property?

我有两个类FooBar Bar扩展Foo如下所示:

class Foo {
    protected 
        $options = array(), 
        $defaults = array(1, 2);
    public function __construct($options){
        array_push($this->options, $this->defaults);
    }
}
class Bar extends Foo {
    protected $defaults = array(3, 4);
    public function print(){
        print_r($this->$options);
    }
}
$bar = new Bar();
$bar->print();

我认为结果应该array(1,2,3,4),但array(3,4).
如何解决?

编辑
我不希望 Bar 类有构造函数,因为我只是超类的实现者,不知道子类中到底会发生什么。

有几个解决方案,最简单的是将第二个变量用作扩展默认值,然后合并数组。

class Foo {
    protected
        $options = array(),
        $original_defaults = array(1, 2),
        $extended_defaults = array();
    public function __construct($options){
        array_merge($this->extended_defaults, $this->original_defaults);
        array_push($this->options, $this->original_defaults);
    }
}
class Bar extends Foo {
    protected $extended_defaults = array(3, 4);
    public function print(){
        print_r($this->$options);
    }
}
$bar = new Bar();
$bar->print();

为什么要组合你的数组?

您将$defaults设置为 (1,2),然后设置为 (3,4) - 您没有在任何地方连接它们。

构造函数将(1,2)添加到$options 上。这就是它所做的一切。

打印方法输出$defaults此时将(3,4),因为您将它们初始化为受保护的变量。

如果您

不希望覆盖这些值,请使用private而不是protected。这将防止子类覆盖这些值。

首先 - 不能有一个名为 print 的方法。 print是一种语言结构,不能被覆盖。

其次,您应该将类的默认值设为私有,并在子类中覆盖它们。 然后,您可以在调用构造函数时将它们组合到父级中。 并不是100%清楚你要完成什么,但以下内容会将子类的默认选项与超类合并:

更新以删除构造函数

abstract class Foo {
    protected $options = array();
    private $defaults = array(1, 2);
   // Implementations of this class MUST define this method
   abstract function overrideDefaults(); 
    public function __construct($options = array()){
        // Merge any incoming options with the default options
        $this->options = array_merge($this->defaults, $options);
    }
    // Concrete children can use this method to modify the current options by 
    // passing in their own defaults.
    protected function modifyDefaults( $defaults ) {
        $this->options= array_merge( $this->defaults, $defaults );
    }
    public function printOps(){
        print_r($this->options);
    }
}
class Bar extends Foo {
    private $defaults = array(3, 4);
    public function overrideDefaults() {
        parent::modifyDefaults( $this->defaults );
    }
}
$bar = new Bar();
$bar->overrideDefaults();
$bar->printOps();

请注意,我还将现在printOps的方法移到了超类。 输出:

Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 ) 
"

我只希望每个子类都可以有它的默认值"。

您希望每个类都有特定的数据。

您可以使用"静态字段",使类本身充当变量。我不太喜欢"静态成员",但我认为,它适用于您的"用例"。

class Foo {
    private 
      // (1) "private" only accessed by the class itself,
      // neither external code, or subclasses,
      // (2) "static", specific to the class,
      static $defaults = array(1, 2);
    protected 
      // want to be accessed only by class & subclasses
      $options = array();
    // when using "static fields" in constructor,
    // you need to override constructor
    public function __construct($options){
        array_push($this->options, Foo::$defaults);
    }
    // ops, "print" is reserved identifier
    // public function print(){
    public function display_options() {
        print_r($this->$options);
    }
    public function display_defaultoptions() {
      // in order to acccess "static fields",
      // you use the class id, followed by double colon,
      // not "$this->*"
      print_r(Foo::$defaults);
    }
} // class Foo
class Bar extends Foo {
private 
    // (1) "private" only accessed by the class itself,
    // neither external code, or subclasses,
    // (2) "static", specific to the class,
    static $defaults = array(1, 2);
    // when using "static fields" in constructor,
    // you need to override constructor
    public function __construct($options){
        array_push($this->options, Bar::$defaults);
    }
    public function display_defaultoptions() {
      // in order to acccess "static fields",
      // you use the class id, followed by double colon
      // not "$this->*"
      print_r(Bar::$defaults);
    }
} // class Bar
$bar = new Bar();
$bar->print();

干杯