1.2.290. Could Use Class Operator

The class operator is ::class. With a class name as left operator, it provides the full class name.

Classes may also be identified with a string, as a fully qualified name. Using the class operator is a more explicit way to do it.

The ::class operator works with the local use expressions. It also provides a string, which may be further processed. The class operator is also called the ‘scope resolution operator’.

<?php

use A\B\C;

$a = C::class;
$a = \A\B\C::class; // also valid
$object = new $a(); // object of A\B\C.

// string version
$a = '\a\b\c';
$object = new $a(); // object of A\B\C.

?>

See also Scope Resolution Operator (::).

1.2.290.1. Suggestions

  • Replace the string with the class operator

1.2.290.2. Specs

Short name

Classes/CouldUseClassOperator

Rulesets

All, Changed Behavior, Suggestions

Exakat since

2.5.0

PHP Version

All

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

High

Features

class-operator

Available in

Entreprise Edition, Exakat Cloud