cyberfly
6/5/2018 - 7:11 AM

PHP OOP overwrite protected property

 //test class with restricted properties
class Test{
    protected $bar="protected bar";
    private $foo="private foo";
    public function printProperties(){
        echo $this->bar."::".$this->foo;   
     }
}

$testInstance = new Test();
//we can change or read the restricted properties by doing this...
$change = function(){
    $this->bar = "I changed bar";
    $this->foo = "I changed foo";
};

$change->call($testInstance);

$testInstance->printProperties();
//outputs I changed bar::I changed foo in php 7.0