Can you extend a Final defined class in php?
Can you extend a Final defined class in php?
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.
class can declared final to indicate that it cannot be extended; that is, one cannot declare subclasses of a final class. A final class cannot be extended. Final method can not be override by the child class. Class variables cannot be declare as final.
Can you extend a Final defined class in php?
A final class is a class that cannot be extended. To declare a class as final, you need to prefix the ‘class’ keyword with ‘final’. Example below.
final class BaseClass {
public function myMethod() {
echo "BaseClass method called";
}
}
//this will cause Compile error
class DerivedClass extends BaseClass {
public function myMethod() {
echo "DerivedClass method called";
}
}
$c = new DerivedClass();
$c->myMethod();
|
Bookmarks