首页 > 其他 > 详细

Visibility from other objects

时间:2016-09-12 06:07:00      阅读:205      评论:0      收藏:0      [点我收藏+]

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

原文:http://www.cnblogs.com/yuanjiangw/p/5863349.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!