PHP Syntax OOP Classes Components Methods Custom Scope Final

 

- Info:

- Method is declared as final by using keyword final.

- Marking a method as final prevents it from being overridden by a subclass.

- This means that you can't create method with the same name and number and type of parameters as in superclass.

 

- Example:

- Creat PHP Console Application Test.php.

 

Test.php

<?php

//CLASS PERSON.-------------------------------------------------------

class Person {

final function displayName() { echo "John"; }

}

//CLASS SOLDIER.------------------------------------------------------

class Soldier extends Person {

//public function displayName() { echo "John"; } //Forbiden to verride method.

}

//USE CLASS.----------------------------------------------------------

$john = new Soldier(); //Create Object from Class.

$john->displayName(); //Call protected method from superclass.

?>