1.2.347. Detect Current Class

Detecting the current class should be done with self::class or static::class operator.

__CLASS__ may be replaced by self\:\:class. get_called_class() may be replaced by static\:\:class.

__CLASS__ and get_called_class() are set to be deprecated in PHP 7.4.

<?php

class X {
    function foo() {
        echo __CLASS__."\n";          // X
        echo self::class."\n";        // X

        echo get_called_class()."\n";  // Y
        echo static::class."\n";       // Y
    }
}

class Y extends X {}

$y = new Y();
$y->foo();

?>

See also PHP RFC: Deprecations for PHP 7.4.

1.2.347.1. Suggestions

  • Use the self::class operator to detect the current class name, instead of __CLASS__ and get_class().

  • Use the static::class operator to detect the current called class name, instead of get_called_class().

1.2.347.2. Specs

Short name

Php/DetectCurrentClass

Rulesets

All, CE, Changed Behavior, CompatibilityPHP74, Suggestions

Exakat since

1.3.8

PHP Version

With PHP 8.0 and older

Severity

Time To Fix

Precision

Very high

Features

class

Available in

Entreprise Edition, Community Edition, Exakat Cloud