Search code examples
phpoop

Constructor Property Promotion access to private variables


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!!
    }
}

Solution

  • 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:

    class Parent {
        protected $protected;
        private $private;
    
        public function __construct(
            $protected,
            $private
        ) {
            $this->protected = $protected;
            $this->private = $private;
        }
    }
    
    class Child extends Parent {
        protected $protected;
        private $private;
    
        public function __construct(
            $protected,
            $private
        ) {
            $this->protected = $protected;
            $this->private = $private;
        }
    
        function myFunc() {
            echo $this->private; // This works!!
        }
    }
    

    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 in Parent, you'll find that it's uninitialised: you didn't run the Parent constructor, so nothing wrote to it.