1.2.589. Interfaces Don’t Ensure Properties

When using an interface as a type, properties are not enforced. They might be not available, and lead to a Fatal Error.

An interface is a template for a class, which specify the minimum amount of methods and constants. Properties are never defined in an interface, and should not be relied upon.

Properties may be defined in an abstract class.

<?php

interface i {
    function m () ;
}

class x implements i {
    public $p = 1;

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

function foo(i $i, x $x) {
    // this is invalid, as $p is not defined in i, so it may be not available
    echo $i->p;

    // this is valid, as $p is defined in $x
    echo $x->p;
}

?>

See also Interface And Abstract Class.

1.2.589.1. Connex PHP features

1.2.589.1.1. Suggestions

  • Use classes for type when properties are accessed

  • Only use methods and constants which are available in the interface

  • Use an abstract class

1.2.589.1.2. Specs

Short name

Interfaces/NoGaranteeForPropertyConstant

Rulesets

All, Analyze, Changed Behavior, Class Review

Exakat since

1.9.5

PHP Version

All

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

High

Available in

Entreprise Edition, Exakat Cloud