If I use Constructor Property Promotion, I can call the constructor automatically. I don’t need write parent::_constructor()
. But the children class can access to the attribute private. That is an error that could be solve just don’t use Propery Promotion. Am I wrong on something?
class Parent {
public function __construct(
protected $protected,
private $private
) {
}
}
class Child extends Parent {
public function __construct(
protected $protected,
private $private
) {
}
function myFunc() {
echo $this->private; // This works!!
}
}
2
Answers
Constructor property promotion just means "declaring the property and the constructor argument at the same time". It doesn’t change how inheritance and private properties work.
Your code is equivalent to this:
The child class is never touching the private property on the parent class, it’s just declaring its own private property that happens to have the same name. If you try to echo
$this->private
in a method defined inParent
, you’ll find that it’s uninitialised: you didn’t run theParent
constructor, so nothing wrote to it.Constructor property promotion is just a shortcut for declaring the properties and assigning them. So
Child
declares its own$protected
and$private
properties, which override the properties inherited fromParent
. SoChild::$private
is private to theChild
class, and can be accessed from methods of theChild
class.Note that you still need to call
Parent::__construct()
. The initialization of theChild
properties doesn’t initialize theParent
properties with the same names. If you don’t call it,Parent
methods that try to access$this->private
or$this->protected
will get an error for the undefined variable.