What is ::class in PHP?

PhpClassLaravel

Php Problem Overview


What is the ::class notation in PHP?

A quick Google search returns nothing because of the nature of the syntax.

colon colon class

What's the advantage of using this notation?

protected $commands = [
    \App\Console\Commands\Inspire::class,
];

Php Solutions


Solution 1 - Php

SomeClass::class will return the fully qualified name of SomeClass including the namespace. This feature was implemented in PHP 5.5.

Documentation: http://php.net/manual/en/migration55.new-features.php#migration55.new-features.class-name

It's very useful for 2 reasons.

  • You don't have to store your class names in strings anymore. So, many IDEs can retrieve these class names when you refactor your code
  • You can use the use keyword to resolve your class and you don't need to write the full class name.

For example :

use \App\Console\Commands\Inspire;

//...

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

Update :

This feature is also useful for Late Static Binding.

Instead of using the __CLASS__ magic constant, you can use the static::class feature to get the name of the derived class inside the parent class. For example:

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

Solution 2 - Php

class is special, which is provided by php to get the fully qualified class name.

See http://php.net/manual/en/migration55.new-features.php#migration55.new-features.class-name.

<?php

class foo {
    const test = 'foobar!';
}

echo foo::test; // print foobar!

Solution 3 - Php

If you're curious in which category it falls into (whether it's a language construct, etc),

It's just a constant.

PHP calls it a "Special Constant". It's special because it's provided by PHP at compile time.

> The special ::class constant is available as of PHP 5.5.0, and allows > for fully qualified class name resolution at compile time, this is > useful for namespaced classes:

https://www.php.net/manual/en/language.oop5.constants.php

Solution 4 - Php

Please be aware to use the following:

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

This will throw a syntax error:

>unexpected 'class' (T_CLASS), expecting variable (T_VARIABLE) or '$'

But you can do the following instead:

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

or

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionYadaView Question on Stackoverflow
Solution 1 - PhpalphayaxView Answer on Stackoverflow
Solution 2 - PhpxdazzView Answer on Stackoverflow
Solution 3 - PhpLucas BustamanteView Answer on Stackoverflow
Solution 4 - PhpHarald WittView Answer on Stackoverflow