一段PHP对象访问类私有方法的代码

来源:PHP 手册

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

posted on 2010-11-05 09:02  陆西星  阅读(938)  评论(0编辑  收藏  举报

导航