<?php
class parentClass {
public function someMethod() { }
}
class childClass extends parentClass {
public final function someMethod() { } //override parent function
}
$class = new childClass;
$class->someMethod(); //call the override function in chield class
?>
Final 키워드
PHP 5는 자식 클래스가 메쏘드나 변수를 덮어 쓰는 것을 막도록, final 키워드를 지원합니다. 정의시에 final을 앞에 붙이면 됩니다.
Example#1 Final 메쏘드 예제
<?php
class BaseClass {
public function test() {
echo "BaseClass::test() 호출\n";
}
final public function moreTesting() {
echo "BaseClass::moreTesting() 호출\n";
}
}
class ChildClass extends BaseClass {
public function moreTesting() {
echo "ChildClass::moreTesting() 호출\n";
}
}
// 결과는 Fatal error: Cannot override final method BaseClass::moreTesting()
?>
Final 키워드
slorenzo at clug dot org dot ve
31-Oct-2007 12:13
31-Oct-2007 12:13
penartur at yandex dot ru
22-Mar-2007 02:39
22-Mar-2007 02:39
Note that you cannot ovverride final methods even if they are defined as private in parent class.
Thus, the following example:
<?php
class parentClass {
final private function someMethod() { }
}
class childClass extends parentClass {
private function someMethod() { }
}
?>
dies with error "Fatal error: Cannot override final method parentClass::someMethod() in ***.php on line 7"
Such behaviour looks slight unexpected because in child class we cannot know, which private methods exists in a parent class and vice versa.
So, remember that if you defined a private static method, you cannot place method with the same name in child class.
