码迷,mamicode.com
首页 > 其他好文 > 详细

Visibility from other objects

时间:2016-09-12 06:09:16      阅读:105      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!