コード例 #1
0
ファイル: index.php プロジェクト: Maelstromeous/learning-php
    public function echoPublic()
    {
        var_dump(__METHOD__, $this->protected);
        $this->echoProtected();
    }
    public function echoPrivateDirectly()
    {
        var_dump(__METHOD__);
        var_dump($this->private);
    }
}
/*
__METHOD__ is known as a magic constant. In the usage here, this allows us to var_dump
the current method we're in, allowing us to see what's calling what in this script.
*/
$class = new Bar();
$class->echoPublic();
// External calls can only call public methods.
# $class->echoProtected(); // would not work here (due to protected scope), but will work inside the class.
$class->echoPrivateDirectly();
// This will fail as the Bar class doesn't have access to $this->private within Foo.
/*
Result:
string 'Bar::echoPublic' (length=15)
string 'Protected' (length=9)
string 'Foo::echoProtected' (length=18)
string 'Private' (length=7)
string 'Bar::echoPrivateDirectly' (length=24)
Notice: Undefined property: Bar::$private
null
*/