How do I get class name in PHP?

How do I get class name in PHP?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Getting the Class Name in PHP: Beyond get_class()

In Java, retrieving the simple name of a class is straightforward using reflection: MyClass.class.getSimpleName(). This syntax feels natural because the .class token explicitly binds to the class object itself. When you transition to PHP, however, the mental model shifts slightly. You likely know get_class($object), but that function requires an instance and returns the name of the instantiated object. In Active Record patterns, where you often need the table name or a unique identifier tied to the class definition without instantiating it, you might be searching for something resembling MyClass::className.

PHP does not provide a magic static property like MyClass::className that automatically returns the string "MyClass". However, there are robust ways to achieve this depending on whether you need the simple name or the fully qualified class path.

The Static Reflection Approach

The most direct equivalent to the Java pattern is using the ReflectionClass API statically. Unlike get_class(), which requires an instance, ReflectionClass::getName() works on the class definition itself.

You can instantiate a reflection object for any class you have access to:

class MyClass {
}

$reflection = new ReflectionClass(MyClass::class);
echo $reflection->getName(); // Outputs: "MyClass"

This returns the simple name of the class, exactly what you are looking for. If your class resides in a namespace, ReflectionClass::getName() will return the fully qualified name including the namespace, whereas ReflectionClass::getShortName() returns only the class name without the namespace prefix.

Handling Namespaces and Fully Qualified Names

When working with complex application architectures, distinguishing between the simple name and the fully qualified name is crucial. The ReflectionClass interface provides specific methods for this distinction.

If you define a class in the global namespace:

class User {
}

$ref = new ReflectionClass(User::class);
echo $ref->getName();    // Outputs: "User"
echo $ref->getShortName(); // Outputs: "User" (same as getName for global scope)

However, if you define a class inside a namespace, such as App\Models\User:

namespace App\Models;

class User {
}

$ref = new ReflectionClass(User::class);
echo $ref->getName();      // Outputs: "App\Models\User"
echo $ref->getShortName();  // Outputs: "User"

This distinction allows you to programmatically determine the table name in an Active Record implementation. You might map the fully qualified name to a specific database prefix or use the short name for logging purposes without exposing the internal namespace structure.

Why __CLASS__ Might Not Be Enough

A common alternative in PHP is the language construct __CLASS__. While this evaluates to a string at compile time, it returns the fully qualified class name (e.g., "App\Models\User"), not just the simple name. If you are using a framework like Laravel or Symfony, the service container often resolves these names automatically, but in raw PHP Active Record implementations, relying solely on __CLASS__ requires string manipulation to strip the namespace if only the short name is needed.

Using ReflectionClass gives you explicit control over what part of the identity you want to retrieve. It is also safer than using string constants defined outside the class context, as the reflection object is bound directly to the class definition at runtime.

Trade-offs and Performance Considerations

While creating a new ReflectionClass instance is a standard operation in PHP, it is not free. If you are retrieving the class name inside a tight loop or within a high-frequency method, instantiating ReflectionClass every time adds overhead. In such scenarios, it is best to cache the result:

class MyClass {
    public static function getShortClassName() {
        $reflection = new ReflectionClass(self::class);
        return $reflection->getShortName();
    }
}

By defining this logic within the class itself and calling it statically, you ensure that the reflection object is created once per request (if cached at the application level) or simply encapsulated cleanly. If performance becomes a bottleneck, frameworks like Laravel often use internal caching mechanisms for these reflections, so external calls are rarely necessary unless you are building a custom ORM layer from scratch.

For most Active Record implementations, the static reflection approach offers the cleanest separation between the class identity and its runtime instance data, bridging the gap between Java-style static access and PHP's dynamic nature.

Our network