1.2.789. No Class As Typehint

Avoid using concrete classes as typehint : always use interfaces or abstract classes. This way, different classes, or versions of classes may be passed as argument. The typehint is not linked to an implementation, but to signatures.

A class is needed when the object is with properties : interfaces do not allow the specifications of properties.

<?php

class X {
    public $p = 1;

    function foo() {}
}

interface i {
    function foo();
}

// X is a class : any update in the code requires changing / subclassing X or the rest of the code.
function bar(X $x) {
    $x->foo();
}

// I is an interface : X may implements this interface without refactoring and pass
// later, newer versions of X may get another name, but still implement I, and still pass
function bar2(I $x) {
    $x->foo();
}

function bar3(I $x) {
    echo $x->p;
}

?>

See also Type hinting for interfaces.

1.2.789.1. Suggestions

  • Create an interface with the important methods, and use that interface

  • Create an abstract class, when public properties are also needed

1.2.789.2. Specs

Short name

Functions/NoClassAsTypehint

Rulesets

All, Typechecks

Exakat since

0.11.4

PHP Version

All

Severity

Major

Time To Fix

Quick (30 mins)

Precision

Very high

Features

typehint, class

Examples

Vanilla, phpMyAdmin

Available in

Entreprise Edition, Exakat Cloud