1.2.959. Parent, Static Or Self Outside Class

Parent, static and self keywords must be used within a class, a trait, an interface or an enum. They make no sense outside a class or trait scope, as self and static refers to the current class and parent refers to one of parent above.

PHP 7.0 and later detect some of their usage at compile time, and emits a fatal error. Static may be used in a function or a closure <https://www.php.net/`closure>`_, but not globally.

<?php

class x {
    const Y = 1;

    function foo() {
        // self is \x
        echo self::Y;
    }
}

const Z = 1;
// This lint but won't anymore
echo self::Z;

?>

1.2.959.1. Suggestions

  • Make sure the keyword is inside a class context

1.2.959.2. Specs

Short name

Classes/PssWithoutClass

Rulesets

All, Analyze, Changed Behavior

Exakat since

0.8.4

PHP Version

All

Severity

Major

Time To Fix

Quick (30 mins)

Changed Behavior

PHP 7.0 - More

Precision

Very high

Features

parent, self, static, class

Available in

Entreprise Edition, Exakat Cloud