gongzhonghao.png
扫码关注我们
最新赞助活动温馨提示:自愿赞助服务器费用,学生和没有工作的整站资源免费下载!
头像

php 类与对象中的访问控制(可见性)

来源:http://erdangjiade.com/topic/2418.html 你好,世界。 2017-09-25 22:42浏览(19)


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

访问同一个对象类型的私有成员

<?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'));?>

//发现:通过传入实例对象,实现了在外部访问私有方法和属性

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

访问同一个对象类型的私有成员

<?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'));?>

//发现:通过传入实例对象,实现了在外部访问私有方法和属性

以上就是php 类与对象中的访问控制(可见性)的详细内容,更多请关注二当家的素材网其它相关文章!

评论0
头像

友情提示:垃圾评论一律封号 加我微信:826096331拉你进VIP群学习群

1 2