Anonymous classes define one-off object implementations without declaring a global class name.
The examples below begin with the core behavior, then move to an application-oriented use.
How PHP Anonymous Classes Works
Anonymous classes define one-off object implementations without declaring a global class name.
Example 1: Basic Usage
This example uses a small input to make the main behavior visible.
<?php
$repository = new class { public function count(): int { return 3; } };
echo $repository->count();
Output:
3
Anonymous classes define one-off object implementations without declaring a global class name.
Example 2: A Different Input
A different set of values shows the same operation in another situation.
<?php
$checker = new class { public function matches(string $value): bool { return $value !== ""; } };
var_dump($checker->matches("hello"));
Output:
bool(true)
Use them for small local implementations; named classes are clearer when behavior grows or is reused.
Practical Example: Applying the Feature
This example shows an application-style use of the feature, with its surrounding setup visible.
<?php
$formatter = new class { public function format(string $text): string { return strtoupper($text); } };
echo $formatter->format("draft");
Output:
DRAFT
The code is shown for learning. Adjust the class names, inputs and error handling to suit your application.
Important Behavior and Edge Cases
- Use them for small local implementations; named classes are clearer when behavior grows or is reused.
- Anonymous classes define one-off object implementations without declaring a global class name.
- Observe differences in empty inputs, invalid states and optional extension availability where relevant.
Comparison With Related Approaches
| Approach | Important Difference |
|---|---|
PHP Anonymous Classes | Anonymous classes define one-off object implementations without declaring a global class name. |
| Alternative approach | A named class is easier to find and share, whereas an anonymous class keeps local glue code nearby. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
Ignoring the documented state or precondition of PHP Anonymous Classes | Use them for small local implementations; named classes are clearer when behavior grows or is reused. |
| Expecting a different type of result | Read the declared return type and distinguish returned values from changes to the source object. |
| Assuming a related API works identically | A named class is easier to find and share, whereas an anonymous class keeps local glue code nearby. |
Key Takeaways
- Anonymous classes define one-off object implementations without declaring a global class name.
- Use them for small local implementations; named classes are clearer when behavior grows or is reused.
- Use the example that matches the values, runtime version and dependencies in your application.
PHP Anonymous Classes is useful when its behavior matches the data structure and lifecycle of your PHP application. Choose it with a clear understanding of its preconditions and returned values.