class MyClass { // some code } $ref = new ReflectionClass('MyClass'); echo $ref->getNamespaceName(); // output: ""
namespace MyLibrary; class MyClass { // some code } $ref = new ReflectionClass('MyLibrary\MyClass'); echo $ref->getNamespaceName(); // output: "MyLibrary"In this example, we define a class MyClass in the namespace MyLibrary. When we pass the fully qualified class name (including namespace) to ReflectionClass, getNamespaceName() will return "MyLibrary", which tells us that this class is part of the MyLibrary package library. By using getNamespaceName() in combination with other ReflectionClass methods, we can gain insight into the structure of a PHP library or application, and use that information to better understand and maintain our code.