In the Child class, I’d need to access the parent property “$this->lang” and use it for other properties, before calling the parent constructor again, like :
abstract class mySuperClass {
protected $lang;
public function __construct($arg1=null, $arg2=null, $arg3=null) {
$this->lang = "US";
}
}
class myChild extends mySuperClass {
public function __construct($arg1=null, $arg2=null, $arg3=null) {
$new_arg1 = $this->lang; // HERE WE ARE STRUGGLYING ...
echo "<br/>step 1) ". get_class(). " : lang= $new_arg1 "; // returns "" where I would need "US"
parent::__construct($new_arg1 , $arg2, $arg3) ;
echo "<br/>step 2) ". get_class(). " : lang= $this->lang"; // returns "US"
}
}
$obj = new myChild("foo1", "foo2", "foo3") ;
This does not look possible/allowed as the parent class is not instanciated at this stage where we are trying to call the parent property $this->lang. Which seems logical.
So the question is :
Is there a way to access the parent property in the child class, with a constructor override as shown ?
If you are statically setting these things anyway (i.e. using hard-coded strings), then make it (the default at least) a static property (possibly even a class constant). Then reference the parent’s static var in the child’s constructor (and in the parent’s, too)