首页 > 代码库 > Visibility from other objects

Visibility from other objects

php.net

 

<?php class Test {     private $foo;     public function __construct($foo)     {         $this->foo=$foo;     }     public 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‘));

 

D:\wamp64\www\w\w.php:20:string ‘hello‘ (length=5)Accessed the private method.

 

Objects of the same type will have access to each others private and    protected members even though they are not the same instances. This is  because the implementation specific details are already known when inside  those objects.

同一个类的对象即使不是同一个实例也可以互相访问对方的私有与受保护成员。这是由于在这些对象的内部具体实现的细节都是已知的。

 

Visibility from other objects