PHP Abstract Class


PHP OOP Abstract Classes

Abstact classes use the abstract keyword. It would start as shown below.
abstract class Myabstract{

You cannot instantiate an abstract class. Thus '$myclass = new Myabstract;' does not work. Abstract classes use abstract methods.

Abstract classes are used when you want to make sure that each new child class uses its own method; even though the method exists in the parent class.

Abstract Method

An abstract method would be written like any other method except it contains 'abstract' at the beginning. When an abstract method is declared in the parent class, the child classes must their own function under the same name as declared in the parent class.

Example:

abstract class Myabstract{

abstract function get_var();
}

class childa extends Myabstract{

function get_var() {
echo "Hello World";
}

}

$mychilda = new childa;
$mychilda->get_var();