Grocker
Grocker

Reputation: 49

Php class inheritance and rewrite?

<?php
class A {
    private function foo(){
        echo 1;
    }
    public function test(){
        $this->foo();
    }
}
class B extends A{
    public function  foo(){
        echo 0;
    }
}
$b = new B();
echo $b->test();

Class B inherits from class A. Why does the output result in 1 instead of 0?

Upvotes: 2

Views: 59

Answers (1)

Rohit Aggarwal
Rohit Aggarwal

Reputation: 650

Because private methods can't be overridden. So, when A.foo is defined again in class B its scope is different and A.test can only see A.foo

Upvotes: 3

Related Questions