Wednesday, 27 January 2016

oop - Accessing private from static method in PHP



Why this works? I mean, accessing the private variable.




class Test {
private $q = 0;
public function __construct() {
$this->q = 1;
}
public static function EpicConstruct() {
$test = new self();
$test->q = 2;
return $test;
}
}

$test = Test::EpicConstruct();

Answer



Because you are accessing the member in the correct context, namely: the class that defines the private member.


No comments:

Post a Comment