1
내가 자기의 차이를 배우고, 정적 및 이것과는 php.net에서이 예제 가로 질러 :
이<?php
class A {
private function foo() {
echo "success!\n";
}
public function test() {
$this->foo();
static::foo();
}
}
class B extends A {
/* foo() will be copied to B, hence its scope will still be A and
* the call be successful */
}
class C extends A {
private function foo() {
/* original method is replaced; the scope of the new one is C */
}
}
$b = new B();
$b->test();
$c = new C();
$c->test(); //fails
?>
결과는
success!
success!
success!
Fatal error: Call to private method C::foo() from context 'A' in /tmp/test.php on line 9
내 질문이입니다 : 새로운 오브젝트 C를 만들 때 , $this->foo();
클래스 C 내부에서 새롭게 바뀐 private function foo를 호출하면 안됩니다 (그리고 private이기 때문에 오류를 리턴합니까)?
흥미 롭습니다! 그러나 오류 메시지가 정확히 예상 한 내용이 아닙니까? 스크립트는 C : foo()를 호출하려고하는데 범위가 A 내에있는 반면 foo()는 이제 C에 비공개이기 때문에 제대로 마주 치지 않습니다. –