1.2.305. Could Use self

self keyword refers to the current class, or any of its parents. Using it is just as fast as the full class name, it is as readable and it is will not be changed upon class or namespace change.

It is also routinely used in traits : there, self represents the class in which the trait is used, or the trait itself.

<?php

class x {
    const FOO = 1;

    public function bar() {
        return self::FOO;
// same as return x::FOO;
    }
}

?>

See also Scope Resolution Operator (::).

1.2.305.1. Suggestions

  • Replace the explicit name with self

1.2.305.2. Specs

Short name

Classes/ShouldUseSelf

Rulesets

All, Analyze, Class Review, Suggestions

Exakat since

0.8.4

PHP Version

All

Severity

Minor

Time To Fix

Instant (5 mins)

Precision

High

Features

self, class

Examples

WordPress, LiveZilla

Available in

Entreprise Edition, Exakat Cloud