1.2.338. Dependant Abstract Classes

Abstract classes should be autonomous. It is recommended to avoid depending on methods, constant or properties that should be made available in inheriting classes, without explicitly abstracting them.

The following abstract classes make usage of constant, methods and properties, static or not, that are not defined in the class. This means the inheriting classes must provide those constants, methods and properties, but there is no way to enforce this.

This may also lead to dead code : when the abstract class is removed, the host class have unused properties and methods.

<?php

// autonomous abstract class : all it needs is within the class
abstract class c {
    private $p = 0;

    function foo() {
        return ++$this->p;
    }
}

// dependant abstract class : the inheriting classes needs to provide some properties or methods
abstract class c2 {
    function foo() {
        // $p must be provided by the extending class
        return ++$this->p;
    }
}

class c3 extends c2 {
    private $p = 0;
}
?>

See also Dependant Trait.

1.2.338.1. Suggestions

  • Make the class only use its own resources

  • Split the class in autonomous classes

  • Add local property definitions to make the class independent

1.2.338.2. Specs

Short name

Classes/DependantAbstractClass

Rulesets

All, Analyze, Class Review

Exakat since

1.8.6

PHP Version

All

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

High

Features

abstract

Available in

Entreprise Edition, Exakat Cloud