Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is ::class in PHP?

Tags:

php

class

laravel

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,
];
like image 738
Yada Avatar asked Jun 11 '15 01:06

Yada


People also ask

What does :: class do in PHP?

::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.

What does :: mean in PHP?

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.

What is :: VS --> in PHP?

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).

What does :: class mean in laravel?

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.


4 Answers

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
like image 143
alphayax Avatar answered Oct 06 '22 00:10

alphayax


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!
like image 44
xdazz Avatar answered Oct 06 '22 00:10

xdazz


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

like image 37
Lucas Bustamante Avatar answered Oct 06 '22 00:10

Lucas Bustamante


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) {...}
like image 22
Harald Witt Avatar answered Oct 05 '22 22:10

Harald Witt