Appending to an array variable in a parent class in PHP


Appending to an array variable in a parent class in PHP



How do I extend my parent's options array for child classes in PHP?

I have something like this:

class ParentClass {       public $options = array(           'option1'=>'setting1'      );       //The rest of the functions would follow } 

I would like to append to that options array in a child class without erasing any of the parent options. I've tried doing something like this, but haven't quite got it to work yet:

class ChildClass extends ParentClass {       public $options = parent::options + array(           'option2'=>'setting2'      );       //The rest of the functions would follow } 

What would be the best way to do something like this?




Singleton with Arguments in Java

1:



In terms of OO design, can two-d and the three-d point classes be derived from single base class?
I think it is better practice to initialize this property in the constructor and then you can extend the value in any descendant class:.
What is the difference between class and instance methods?
<?php class ParentClass {   public $options;  public function __construct() {   $this->options = array(       'option1'=>'setting1'    );   }   //The rest of the functions would follow } class ChildClass extends ParentClass {   public function __construct() {    parent::__construct();    $this->options['option2'] = 'setting2';  }   //The rest of the functions would follow } ?> 


Dealing with multiple-inherited constructors in Moose


Accessing parent properties and overridden methods in PHP

2:



Instantiating child classes from parent class (PHP)
PHP or no, you should have an accessor to do it, so you can call $self->append_elements( 'foo' => 'bar' ); and not worry about the internal implementation..
Turning off inline constructors with MooseX::Declare


Object doesn't do anything after instantiation - bad code smell?

3:


Can you array_merge?. Assuming you use the ctr to create the class.. E.g..
public function __construct(array $foo) {   $this->options = array_merge(parent::$options, $foo); } 



82 out of 100 based on 72 user ratings 622 reviews

*