class_basename
Returns a class name with its namespace stripped, from either a string or an object.
class_basename(string|object $class): stringParameters
| Parameter | Type | Description |
|---|---|---|
$$class | string|object | A fully qualified class name, or an object whose class is used. |
Return value
stringThe portion of the class name after the final backslash.
Errors
Nothing; the class is never autoloaded or verified, so an unknown name returns its own short form rather than erroring.
class_basename() takes a class-string or an object and gives you the part after the last backslash. The implementation is deliberately trivial: it converts an object to its class name with get_class(), swaps backslashes for forward slashes, and calls PHP's basename().
When to reach for it
Anywhere a fully qualified name is too noisy for a human or too long for a key: naming a broadcast event, deriving a permission or table prefix from a model, labelling a queued job in a log line, or building a view name from a class.
What trips people up
It is string manipulation, not reflection. It never autoloads, never checks that the class exists, and never resolves a use alias — class_basename('does\\not\\Exist') cheerfully returns Exist. A typo produces a plausible-looking short name instead of an error, which is exactly how a wrong event name reaches production.
Anonymous classes are the ugly case: get_class() returns something like class@anonymous/path/to/file.php:12$0, and after the slash swap basename() keeps only the fragment after the last separator. Never build a stable identifier from one.
When you need real reflection semantics — including a hard failure on an unknown class — use (new ReflectionClass($class))->getShortName().
Example
When to use it
- Derive a broadcast event name from the event class in broadcastAs().
- Log a queued job by its short class name instead of the full namespace.
- Build a permission or cache key such as 'view-Post' from a model class-string.
Frequently asked questions
How do I get a class name without the namespace in Laravel?
Call class_basename($classOrObject), which returns everything after the final backslash. It accepts either a fully qualified class-string or an object, so class_basename($this) works inside a class.
Does class_basename() work on objects?
Yes. If you pass an object it calls get_class() first, so class_basename(new App\Models\Post) returns Post. Anonymous classes are the exception — their generated names contain a file path and produce unusable output.
Does class_basename() check that the class exists?
No. It performs no autoloading and no validation, so a misspelled or entirely fictional class name returns its own short form. Use (new ReflectionClass($class))->getShortName() when you need the lookup to fail loudly.
More examples
Naming things from a model class
This is the chain Laravel itself uses to guess a table name from a model. class_basename() removes the namespace, Str::snake() converts the studly case, and Str::plural() pluralises only the final word — which is why blog_post becomes blog_posts rather than blogs_post.
<?php
use Illuminate\Support\Str;
$model = 'App\Models\BlogPost';
echo class_basename($model), "\n"; // BlogPost
echo Str::snake(class_basename($model)), "\n"; // blog_post
echo Str::plural(Str::snake(class_basename($model))), "\n"; // blog_posts
echo Str::kebab(class_basename($model)), "\n"; // blog-postA broadcast event name
Without broadcastAs(), the client would have to subscribe to App\Events\OrderShipped, backslashes and all. Passing $this rather than a string means the name follows the class if it is ever renamed.
<?php
namespace App\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
class OrderShipped implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets;
public function broadcastOn(): Channel
{
return new Channel('orders');
}
public function broadcastAs(): string
{
echo class_basename($this), "\n"; // OrderShipped
return class_basename($this);
}
}Reflection is the stricter alternative
class_basename() returns Up for a class that was never defined, which is fine when you only want a label but dangerous when the result is used to look something up. ReflectionClass throws instead, so use it whenever a wrong name must be caught rather than propagated.
<?php
// No error — it is just string manipulation.
echo class_basename('Totally\Made\Up'), "\n"; // Up
// Reflection actually resolves the class and fails when it cannot.
try {
echo (new ReflectionClass('Totally\Made\Up'))->getShortName(), "\n";
} catch (ReflectionException $e) {
echo 'ReflectionException: class does not exist', "\n";
}
Discussion