Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does ClassName::class mean in PHP? [duplicate]

Tags:

php

laravel

When I read code written in the Laravel framework, I see a lot of uses of ClassName::class. What does is the meaning of the modifier ::class? Is this a feature of Laravel or PHP? I've searched but been unable to find documentation.

like image 920
yinwei Avatar asked Sep 08 '15 02:09

yinwei


People also ask

What does :: class mean in PHP?

Since PHP 5.5, the class keyword is also used for class name resolution. You can get a string containing the fully qualified name of the ClassName class by using ClassName::class. This is particularly useful with namespaced classes.

What is 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 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.


1 Answers

Since PHP 5.5, the class keyword is also used for class name resolution. You can get a string containing the fully qualified name of the ClassName class by using ClassName::class. This is particularly useful with namespaced classes.

For example

namespace MyProject; class Alpha{ }  namespace MyOtherProject; class Beta{ }  echo Alpha::class; // displays: MyProject\Alpha echo Beta::class; // displays: MyOtherProject\Beta 
like image 135
DeDee Avatar answered Oct 09 '22 01:10

DeDee