Most PHP code reaches for a full class definition out of habit. That's fine, until you need a class only once. PHP anonymous classes let you spin up a class on the fly, use it, and discard it. This piece walks through a memoize helper that uses anonymous classes, then shows what you gain—and what you trade away.
The short version
- PHP anonymous classes define a class inline without naming it.
- They're useful when you need class features like __call() but only once.
- The reference uses anonymous classes to build a drop-in memoize() helper.
- They cut boilerplate when a standalone class would be overkill.
How the pieces fit together
First, the use case. The reference describes a memoize helper that caches function results. Without anonymous classes, you'd need a full Memoize class with a __call() magic method, a constructor, and instantiation. That's significant overhead for a one-line helper.
Next, the anonymous class. The reference uses new class ($target, $memo) with a constructor and a __call() method. The class has no name, exists only inside the function, and gets discarded when the function returns.
Then, the __call() magic method. The reference shows how __call() intercepts method calls on the target object, builds a signature from method name plus a hash of parameters, and either returns the cached result or computes and caches a new one. Without __call(), you couldn't proxy arbitrary methods transparently.
| Anonymous Class | Named Class | |
|---|---|---|
| Scope | Function or method | Global |
| Naming | None | Required |
| Instantiation | Inline, returns object | Requires class definition first |
| Drop-in usage | Yes, in helper functions | No, requires separate file or autoload |
| Use case | One-off, short-lived | Reusable, persistent |
Why this matters
Anonymous classes shift the burden. Instead of defining a class, registering it, and instantiating it, you write one expression that returns a working object. For helpers, decorators, and proxies, this means less code and clearer intent—the behavior lives where it's used.
The trade-off is scope. Anonymous classes can't be autoloaded or referenced by name. They live and die with their enclosing function. If your object needs to outlive a single call, use a named class.
Who wins:** Developers writing helpers, one-off proxies, or temporary mocks. The code stays concise.
Who loses:** Developers who need reusable, autoloaded classes. Anonymous classes don't fit that mold.
What to watch:** Whether your "temporary" object actually needs to persist. If it does, anonymous classes will fight you.
Bottom line
Anonymous classes are a small feature with a specific job: one-off class behavior inside a function or method. They pair well with __call() and __invoke(). Use them when a named class would be overkill, and avoid them for anything that needs to live longer than the function call.
Key takeaways
- Anonymous classes are for one-off class definitions inside a function.
- They pair well with __call() and __invoke() magic methods.
- They cut boilerplate when a named class would be overkill.
- Don't use them for classes that need autoloading or a service container.
Frequently asked questions
What is an anonymous class in PHP?
It is a class defined inline with new class { … } that has no name and exists only inside the scope where it's created.
When should I use an anonymous class?
Use it when you need class features like magic methods but only once, such as in a helper function or a short-lived proxy object.
How do anonymous classes work with __call()?
They work the same as named classes. Define the magic method on the anonymous class and intercept method calls as usual.
Can anonymous classes be autoloaded?
No. They have no name, so they can't be referenced or autoloaded. Use named classes for anything that needs to persist.