1.2.9. ::class

PHP has a special class constant to hold the name of the class : class keyword. It represents the class name that is used in the left part of the operator.

Using \:\:class is safer than relying on a string. It does adapt if the class’s name or its namespace is changed’. It is also faster, though it is a micro-optimisation.

It is introduced in PHP 5.5. Be aware that \:\:class is a replacement for __CLASS__ magic constant.

<?php

use A\B\C as UsedName;

class foo {
    public function bar( ) {
        echo ClassName::class;
        echo UsedName::class;
    }
}

$f = new Foo( );
$f->bar( );
// displays ClassName
// displays A\B\C

?>

See also Class Constant.

1.2.9.1. Connex PHP features

1.2.9.1.1. Suggestions

  • Use ::class whenever possible. That exclude any dynamic call.

1.2.9.1.2. Specs

Short name

Php/StaticclassUsage

Rulesets

All, Changed Behavior, CompatibilityPHP53, CompatibilityPHP54

Exakat since

0.8.4

PHP Version

With PHP 5.5 and more recent

Severity

Major

Time To Fix

Slow (1 hour)

Precision

Very high

Available in

Entreprise Edition, Exakat Cloud