PHP Syntax OOP Classes Types Abstract


- Info:

- Abstract class is declared by using keyword abstract.

- Abstract class is intended to be inherited by other classes abd therefore can't be instantiated.

- Only abstract classes can have abstract methods.

- Abstract methods are declared only within a parent class but are implemented in child classes.

- Only classes declared as abstract can contain abstract methods.

- You can't define method implementation in parent class.

- You must define method implementation in child classes.


- Example:

- This example shows how to declare abstract method in superclass and implement it in subclass.

- Creat PHP Console Application Test.php.




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

abstract class Person {

abstract function displayName(); //No implementation.


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

class Soldier extends Person {

public function displayName() { echo "John"; } //Must define implementation.


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

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

$john->displayName(); //Call implemented abstract.