1.2.579. Instantiating Abstract Class

PHP cannot instantiate an abstract class.

The classes are actually abstract classes, and should be derived into a concrete class to be instantiated.

<?php

abstract class Foo {
    protected $a;
}

class Bar extends Foo {
    protected $b;
}

// instantiating a concrete class.
new Bar();

// instantiating an abstract class.
// In real life, this is not possible also because the definition and the instantiation are in the same file
new Foo();

?>

See also Class Abstraction.

1.2.579.1. Suggestions

  • Make the class non abstract

  • Extends that class with a new class that is not abstract. Instantiate that second class.

  • Find an existing concrete class

1.2.579.2. Specs

Short name

Classes/InstantiatingAbstractClass

Rulesets

All, Analyze

Exakat since

0.8.4

PHP Version

All

Severity

Major

Time To Fix

Quick (30 mins)

Precision

Very high

Features

abstract, concrete

Available in

Entreprise Edition, Exakat Cloud