-1
class Configurable
{
    protected function configure(array $config): void
    {
        foreach ($config as $key => $value){
             if (property_exists($this, $key)){
                $this->{$key} = $value;
            }
        }
    }
}

class C extends Configurable
{
    private $abc;

    public function __construct()
    {
        $this->configure(['abc' => 5]);
    }
}

$c = new C();

This code gives me error Cannot access private property C::$abc

It is obvious when private variable in parent class but in this code the private variable is in child class and property_exists sees this variable. I can not find explanation in php docs.

My confusion is as follows. Parent method is inherited in child. My assumption is that this method should have access to variable in child, but it does not have. property_exists knows about this property, but can not set it.

Vitaliy
  • 429
  • 1
  • 4
  • 14

1 Answers1

0

use protected$abc` visibility cause of you're using parent method(configure()) in same class so in parent class abc variable can't be access cause of private.

class Configurable
{
    protected function configure(array $config): void
    {
        foreach ($config as $key => $value){
             if (property_exists($this, $key)){
                $this->{$key} = $value;
            }
        }
    }
}

class C extends Configurable
{
    protected $abc;

    public function __construct()
    {
        $this->configure(['abc' => 5]);
    }

    //this method is when you're trying to access $c->abc that'll return from here.
    public function __get($method)
    {
        return $this->$method;
    }
}

$c = new C();
echo $c->abc;

And if you want to use Private to define variable then you sould define configure() method in same class. and __get method is for when you're trying to access protected and private variable then this __get() function will called.

class Configurable
{
    protected function configure(array $config): void
    {
        foreach ($config as $key => $value){
             if (property_exists($this, $key)){
                $this->{$key} = $value;
            }
        }
    }
}

class C extends Configurable
{
    private $abc;

    public function __construct()
    {
        $this->configure(['abc' => 5]);
    }

    //method overriding
    protected function configure(array $config): void
    {
        foreach ($config as $key => $value){
             if (property_exists($this, $key)){
                $this->{$key} = $value;
            }
        }
    }

    public function __get($method)
    {
        return $this->$method;
    }
}

$c = new C();

echo $c->abc;
Dilip Hirapara
  • 14,810
  • 3
  • 27
  • 49
  • Your second example beats the purpose of inheritance and contradicts SOLID. I recommend @Vitaliy against adopting it. He should just use `protected` instead of `private`. – Aydin4ik Aug 08 '19 at 07:14
  • Agree with you @Aydin4ik but he's trying to access variable using private that's why I made second approach just only for him. – Dilip Hirapara Aug 08 '19 at 07:17