Coder Perfect

In PHP, what is::class?


In PHP, what does the::class notation mean?

Because of the syntax’s nature, a fast Google search yields nothing.

colon colon class

What are the benefits of this notation?

protected $commands = [

Asked by Yada

Solution #1

The fully qualified name of SomeClass, including the namespace, is returned by SomeClass::class. PHP 5.5 was the first version to include this feature.


It’s quite beneficial for two reasons.

For example :

use \App\Console\Commands\Inspire;


protected $commands = [
    Inspire::class, // Equivalent to "App\Console\Commands\Inspire"

Update :

Late Static Binding can also benefit from this feature.

You can access the name of the derived class inside the parent class using the static::class feature instead of the __CLASS__ magic constant. Consider the following scenario:

class A {

    public function getClassName(){
        return __CLASS__;

    public function getRealClassName() {
        return static::class;

class B extends A {}

$a = new A;
$b = new B;

echo $a->getClassName();      // A
echo $a->getRealClassName();  // A
echo $b->getClassName();      // A
echo $b->getRealClassName();  // B

Answered by alphayax

Solution #2

class is a unique feature of PHP that allows you to acquire the fully qualified class name.



class foo {
    const test = 'foobar!';

echo foo::test; // print foobar!

Answered by xdazz

Solution #3

If you’re curious about which category it belongs to (e.g., whether it’s a linguistic construct or not), click here.

It’s just a fact of life.

It’s referred to as a “Special Constant” in PHP. It’s unique in that PHP provides it at compile time.

Answered by Lucas Bustamante

Solution #4

Please keep the following in mind:

if ($whatever instanceof static::class) {...}

This will result in the following syntax error:

You can, however, perform the following instead:

if ($whatever instanceof static) {...}


$class = static::class;
if ($whatever instanceof $class) {...}

Answered by Harald Witt

Post is based on