一段PHP对象访问类私有方法的代码

网络整理 - 08-29

<?phpclass Test{ private $foo; public function __construct($foo) { $this->foo = $foo; } private function bar() { echo 'Accessed the private method.'; } public function baz(Test $other) { // We can change the private property: $other->foo = 'hello'; var_dump($other->foo); // We can also call the private method: $other->bar(); }}$test = new Test('test');$test->baz(new Test('other'));?>