PHP Syntax OOP Classes Components Methods Custom Scope Abstract

 

- Info:

- Method is declared as abstract by using keyword abstract.

- 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.

 

Test.php

<?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.

?>