Examples:
Final methods example
PHP 5 introduces the final keyword, which prevents child classes from overriding a method by prefixing the definition with final. If the class itself is being defined final then it cannot be extended.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <?php class BaseClass { public function test() { echo "BaseClass::test() called\n" ; } final public function moreTesting() { echo "BaseClass::moreTesting() called\n" ; } } class ChildClass extends BaseClass { public function moreTesting() { echo "ChildClass::moreTesting() called\n" ; } } // Results in Fatal error: Cannot override final method BaseClass::moreTesting() ?> |
Final class example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <?php final class BaseClass { public function test() { echo "BaseClass::test() called\n" ; } // Here it doesn't matter if you specify the function as final or not final public function moreTesting() { echo "BaseClass::moreTesting() called\n" ; } } class ChildClass extends BaseClass { } // Results in Fatal error: Class ChildClass may not inherit from final class (BaseClass) ?> |
Please login to continue.