PHP中从静态方法访问私有

PHP中从静态方法访问私有,php,oop,static-methods,Php,Oop,Static Methods,为什么会这样?我是说,访问私有变量 class Test { private $q = 0; public function __construct() { $this->q = 1; } public static function EpicConstruct() { $test = new self(); $test->q = 2; return $test; } } $test = T

为什么会这样?我是说,访问私有变量

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(); 课堂测试{ 私人$q=0; 公共函数构造(){ $this->q=1; } 公共静态函数{ $test=新的self(); $test->q=2; 返回$test; } } $test=test::EpicConstruct();
因为您是在正确的上下文中访问成员的,即:定义私有成员的类。

因为您是从同一个类访问该成员的。请将其视为男女更衣室。同一类的对象可以看到彼此的隐私,所以我可以用这种方式处理这个类的任何对象?我想我现在了解了装饰图案的工作原理。谢谢@smsteel只要是同一等级:是的。对于decorator模式,情况并非如此:因为decorator是一个扩展类的类,或者更典型地,是一个扩展/实现公共超类型(一些基类或接口)的类。前一种装饰器只能访问至少受
保护的
成员,后一种装饰器只能访问类的
公共
成员(或受
保护的
超类型成员)。