Final method ad final class
Final method
- it is used to prevent the child class to re-implement the function in parent class
- If derived class try to re-implent the final method, php will give error to the user.
Example:
class Base{
final public function test() {
echo “From the base class”;
}
}
In the above example function test is prefixed by final. SO it can’t be overrided by the child class
final class
- If the class is defined with prefix `final`, then it can’t be inherited
Example:
final class Base{
public function test() {
echo “From the base class”;
}
}
In the above example class Base is prefixed by final, so it can’t be inherited. If anyother class try to inherit the above class, php will give the error.
