← back to code snippets


Abstract class order

Posted on November 19th, 2013 by Graeme

The abstract class must be included before the extending class, and the extending class cannot be instantiated until before its definition. 

For example, my class wouldn’t instantiate when I had:

require "Animal.php";//abstract class

$human = new Human();

Class Human extends Animal
{
//class definition
}

To correct this, I had to do: 

require "Animal.php";//abstract class

Class Human extends Animal
{
//class definition
}

$human = new Human();

Notice that “new Human();” is moved under the class definition of Human.

It would be better practice to instantiate the class in a separate file, just using a require line for each class.