Software Development

Return class’ Canonical Name in PHP

Background

[wp_ad_camp_1]

There are times you need to refer to a class by a string representing its fully qualified class name. For example in Laravel 4.2, you may specify a route to a controller by hard-coding the fully qualified class name. However, this is not a good practice as classes may be moved to different namespaces overtime specially during the development stage. Since I am from a Java background, I tend to reuse some terms and terminologies in other platform. In Java speak, a class’ canonical name is simply its fully qualified class name. So, this article simply demonstrates how to retrieve a class’ fully qualified name.

Software Requirements

  • Windows 7 Professional SP1
  • PHP 5.6.3 / Zend Engine v2.6.0
  • NetBeans IDE 8.0.1

Canonical Name equals Fully Qualified Class Name

There are two scenarios where you can get a class’ canonical name – via the class itself and an instance of that class.

Get canonical name statically

[wp_ad_camp_2]

Get canonical name via instance of a class

[wp_ad_camp_3]

Loading

Got comments or suggestions? We disabled the comments on this site to fight off spammers, but you can still contact us via our Facebook page!.


You Might Also Like