143

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,
];
TRiG
  • 10,148
  • 7
  • 57
  • 107
Yada
  • 30,349
  • 24
  • 103
  • 144

4 Answers4

136

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
miken32
  • 42,008
  • 16
  • 111
  • 154
alphayax
  • 2,930
  • 2
  • 25
  • 25
  • 2
    A slight correction: in the first example, `Inspire::class` is equivalent to "App\Console\Commands\Inspire", without the backward slash prefix. – Jason Mar 13 '18 at 13:43
  • 1
    @FabienHaddadi : note that the two notations `use \App\...` and `use App\...` are allowed. I use that to make the difference between a class contained in a subnamespace and a class contained outside the current namespace hierarchy. – alphayax Oct 12 '18 at 12:49
  • 2
    A word of warning, you can type anything and still get a "class" name. I could type SomeDumbCrapThatDoesntExist::class and it won't give me an error or a warning if the IDE doesn't catch it. Easy to make a typo and miss it. – Gabriel Alack Feb 10 '20 at 22:56
23

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!
xdazz
  • 158,678
  • 38
  • 247
  • 274
  • 1
    Is there a reason why Laravel decided to switch to it? – Yada Jun 11 '15 at 01:41
  • 7
    It is not a constant on the Inspire class. It is a constant provided by php. It is a quick way to get the fully qualified class name. http://php.net/manual/en/migration55.new-features.php#migration55.new-features.class-name – jfadich Jun 11 '15 at 01:55
  • 2
    @Yada I believe the reasoning behind Laravel using it is that it gives you one less place to make a typo. You can use the string '\App\Console\Commands\Inspire' or Inspire::class to get the same thing, but your IDE will catch syntax/spelling errors on the latter making it a little easier to debug. – jfadich Jun 11 '15 at 02:03
  • 5
    What does this code have to do with the question? – miken32 May 19 '21 at 20:57
8

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

Lucas Bustamante
  • 15,821
  • 7
  • 92
  • 86
2

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) {...}
  • this hack for dynamic naming, from php 5.* works like `$className = 'SomeCLass'; $className = new $className(); $methodName = 'someMethod'; $className->$methodName($arg1, $arg2, $arg3); /* or if args can be random array*/ call_user_func_array([$className, $methodName], $arg);` – Vladimir Ch Feb 24 '19 at 21:56