The last keyword in PHP

PHP

Example :  
// Program for understanding usage
// last keyword for methods class Base { // Final method final function printdata() { echo "Base class final printdata function " ; } // Not final method function nonfinal() { echo "This is nonfinal function of base class" ; } }  
// A class that extends the base class class Derived extends Base { // Inherit non-final method function nonfinal() { echo "Derived class non final function" ; } // Here the printdata function can // not overridden} $obj = new Derived; $obj -> printdata(); $obj -> nonfinal(); ?>
Exit:
Base class final printdata function Derived class non final function 
Final Classes:A class declared as final cannot be extended in the future. The classes are declared final due to some design level issue. The creator of the class declares this class as final if he wants this class not to inherit for some security or other reason. The final class can contain both final and non-final methods. But the class does not use final methods, when the class itself is declared as final, since inheritance is not possible.Example :  
// Program for understanding graduation classes
// in php final class Base { // Final method final function printdata() { echo "final base class fi nal method " ; } // Not final method function nonfinal() { echo "non final method of final base class" ; } } $obj = new Base; $obj -> printdata(); $obj -> nonfinal();  
/ * If we uncomment these lines,
show derived class cannot inherit from final
class (base)
The derived class extends the base { 
} * /
?>
Exit:
final base class final method non final method of final base class 
Note: inunlike Java, the PHP final keyword can only be used for methods and classes, not for variables.



Get Solution for free from DataCamp guru