Here are the following examples:
Method with final Keyword:
<?php class MyParentClass { public function test() { echo "ParentClass::test() called\n"; } final public function anotherTesting() { echo "BaseClass::anotherTesting() called\n"; } } class MyChildClass extends MyParentClass { public function anotherTesting() { echo "ChildClass::moreTesting() called\n"; } } // Results in Fatal error: Cannot override final method MyChildClass::anotherTesting() ?>
Class with final Keyword:
<?php final class MyParentClass { public function test() { echo "ParentClass::test() called\n"; } final public function anotherTesting() { echo "BaseClass::anotherTesting() called\n"; } } class MyChildClass extends MyParentClass { public function anotherTesting() { echo "ChildClass::moreTesting() called\n"; } } // Results in Fatal error: Class MyChildClass may not inherit from final class (MyParentClass) ?>
As you can see in the two examples above, both method and class can’t be overridden by its subclass. Doing so will throw an exception error and will break your code.
0 comments:
Post a Comment