Re: class method visibility

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



2012/3/15 Jeremy Wei <shuimuqingshu@xxxxxxxxx>:
> I read the manual about  method visibility, but i can't understand the
> code below:
> <?php
> class Bar
> {
>    public function test() {
>        $this->testPrivate();
>        $this->testPublic();
>    }
>
>    public function testPublic() {
>        echo "Bar::testPublic\n";
>    }
>
>    private function testPrivate() {
>        echo "Bar::testPrivate\n";
>    }
> }
>
> class Foo extends Bar
> {
>    public function testPublic() {
>        echo "Foo::testPublic\n";
>    }
>
>    private function testPrivate() {
>        echo "Foo::testPrivate\n";
>    }
> }
>
> $myFoo = new foo();
> $myFoo->test(); // Bar::testPrivate
>                     // Foo::testPublic
> ?>
>
> in class Bar's method test, why "$this->testPrivate();"  invoked
> testPrivate method of class Bar,
> but not the testPrivate of class Foo?  here $this shoud be the
> reference of the instance of class Foo,
> isn't it?
>

Since you're executing this from the class Bar, the only visible
testPrivate function there is Bar::testPrivate. This would work the
way you expect it only if the test() function was inside class Foo.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux