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. Continue reading “PHP Class Name Resolution using ::class”