サブクラスのインスタンスを使用して親クラスのテストメソッドを呼び出しているため、コードの最初の出力が「Bar::testPrivate」を出力する理由がわかりませんでした。したがって、テスト関数内のコードの最初の行を呼び出すと、 「$this->testPrivate();」サブクラスの testPrivate メソッドを呼び出す必要があるため、「Bar::testPrivate」ではなく「Foo::testPrivate」と出力します。
<pre>
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
</pre>