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,
];
::class ¶ The class keyword is also used for class name resolution. To obtain the fully qualified name of a class ClassName use ClassName::class . This is particularly useful with namespaced classes.
In PHP, the double colon :: is defined as Scope Resolution Operator. It used when when we want to access constants, properties and methods defined at class level. When referring to these items outside class definition, name of class is used along with scope resolution operator.
This means that -> is mostly used to access instance members (though it can also be used to access static members, such usage is discouraged), while :: is usually used to access static members (though in a few special cases, it's used to access instance members).
Using the ::class keyword in Laravel Since PHP 5.5 the class keyword is used for class name resolution. This means it returns the fully qualified ClassName. This is extremely useful when you have namespaces, because the namespace is part of the returned name.
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.
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
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!
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
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) {...}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With