首页 > 代码库 > 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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。