PHP Class Name Resolution using ::class

With PHP 7+ being rolled out on many web hosting servers these days, there is an increased push to using Namespacing in PHP coding. This is also very evident in the PHP Standard Recommendations (PSR-0 [deprecated] and PSR-4 [active]) for Auto-loading defined by the PHP Framework Interop Group (PHP-FIG) that everyone seems to be following these days. One of the things that can be observed with the increase in Namespace using is the need to remember or query the fully qualified name of a class or object. This can be tricky at first glance, but evidently, since PHP 5.5, a very useful feature was added to easily get the fully qualified name of a class. This can come in very handy especially if you are creating your own PHP library or implementing a design pattern like a Factory pattern. This feature is called “Class Name Resolution” and it can be achieved by suffixing “::class” at the end of a class name, like this: ClassName::class. To demonstrate the value of this feature, I will show some example code, and also mention some useful functions that do the similar function for objects.

Using ::class feature

The double-colon class feature is very useful when you are trying to create objects dynamically, and it is also useful for checking for the existence of classes and interfaces. You only understand the value of this when you start to extensively use namespaces in your code, especially when you are creating a library or API. It often can become a hassle to remember and correctly type all those Fully Qualified Names for classes when you need an object. Of course, you have the use keyword that comes into play when creating objects. But it is often very helpful to use a Factory design pattern to quickly and seamlessly create objects that you need. The following is some very simple code that you can run to get an idea of how you can use the double-colon class feature:

If you run the code in the example above, you will get the following result:

As you can see by looking at the code, you can specify the Fully Qualified Name (FQN) of the class without having the actually type it out. The only point at which you type it out is at the use keyword. So, in the case of this example, you can specify the FQN for a class in one file. Register the FQN with a Factory. Then, instantiate an object in any other script via the Factory without ever having to specify the FQN again. I think that that feature is really cool.

static::class versus __CLASS__ (and Late Static Binding)

Another very interesting use of double-colon class feature is for finding out the name of the actual class executed rather than the inherited class. The __CLASS__ magic constant is typically used the find out the name of a class. However, this magic constant only accurately works for naming the base or parent class and not a derived class. You would have to override the function using the __CLASS__ magic constant in the derived class if you want to accurately get the classes FQN. However, with the double-colon class feature, we can accurately retrieve the FQN for the class by using it on the static keyword: static::class.

By adding double-colon class to the end of the static keyword (static::class), we are making use of a PHP feature called “Late Static Binding”, which was introduced in PHP 5.3. Essentially, by using the static keyword, with the double-colon class, the class name will not be resolved where the method is defined, but rather it will be resolved from the class that was called at runtime. Here is a modified example to show how this works:

The result of the code above is as follows:

The results reveal that using static::class gets the classname of the derived class every time.

One interesting tip to note is that using the __CLASS__ magic constant is equivalent to using self::class. They produce the same result. You can replace __CLASS__ with self::class in example 2 above, and you will see that the results are the same.

Equivalent functions for objects

Lastly, I just want to point out some equivalent functions that you can use to find out class names:

  1. get_called_class() – This function gets the late static binding class name. It is equivalent to using static::class.
  2. get_class() – This function when used without a parameter, inside a class, will give the class name of the class where the method is defined. It works the same way as using the __CLASS__ magic constant.
  3. get_class($this) – This function when used with the $this keyword will provide the class name of the derived class from which the object was instantiated. It is equivalent to using static::class. Of course, putting any object variable as the parameter instead of $this, will give you the FQN of the object’s class.
  4. class_exists(ClassName::class) – Use this function with ClassName::class to determine if the class has been defined.
  5. is_subclass_of($object, ClassName::class) – Use this function to find out if an object is the child of the specified class name or if it implements it.
  1. PSR-4: Autoloader – PHP-FIG []
  2. Class Name Resolution – []
  3. Magic Constants – []
  4. Late Static Bindings – []
  5. The Basics – []
  6. Namespaces and dynamic language features – []
Posted in PHP

Leave a Reply

Your email address will not be published. Required fields are marked *

Recent Posts



Users Online

1 User Browsing This Page.
Users: 1 Bot


GiottoPress by Enrique Chavez