PDA

View Full Version : Can you extend a Final defined class in php?



Krina
06-27-2018, 03:38 AM
Can you extend a Final defined class in php?

ecozaar
06-27-2018, 04:11 AM
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.

pharmasecure
06-28-2018, 12:56 AM
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.

team_alphaa
06-28-2018, 05:30 AM
Can you extend a Final defined class in php?

rohit
06-30-2018, 02:46 AM
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();