Reputation: 410
Is it somehow possible to modify parent public var which is an array so that in child class it will have more items in it?
For now i'm just repeating parent array physically and then adding some items to it:
class A {
public $arr = ['hello'];
}
class B extends A {
public $arr = ['hello','world']; //wanna get rid of 'hello' and use parent
}
Upvotes: 2
Views: 216
Reputation: 9093
The field declaration should be named $arr
, not arr
. And, you shouldn't re-declare the same field in the child class. You can change it in the constructor:
class A {
public $arr = ['hello'];
}
class B extends A {
public function __construct() {
$this->arr []= 'world';
}
}
A small test:
$a = new A(); print_r( $a->arr );
$b = new B(); print_r( $b->arr );
produces:
Array
(
[0] => hello
)
Array
(
[0] => hello
[1] => world
)
Upvotes: 3