1.2.958. Parent Is Not Static

The parent keyword behaves like self, not like static. It links to the parent of the defining expression, not to the one being called.

This may skip the parent of the calling class, and create a Undefined method call, or yield the wrong ::class value. It may also skip a local version of the method.

<?php

class w {
}

class x extends w {
    function foo() {
        parent::method();
    }

    // method() is in the parent of Y, but not in the one of X.
    function method() {
        print __METHOD__;
    }
}

class y extends x {}

(new y)->foo();
// print W::method
(new y)->method();
// print x::method

?>

1.2.958.1. Suggestions

  • Use self keyword

  • Use static keyword

  • Use hard-coded class name keyword

1.2.958.2. Specs

Short name

Classes/ParentIsNotStatic

Rulesets

All, Analyze, Class Review

Exakat since

2.4.3

PHP Version

All

Severity

Major

Time To Fix

Quick (30 mins)

Precision

Very high

Features

self, static, parent

Available in

Entreprise Edition, Exakat Cloud