Pass a One-Off PHP Object to a Function That Expects an Interface
Use a PHP anonymous class at an interface-typed call site, then extend a base class to reuse a protected method.
If a function needs a Formatter for one call, you can pass it an anonymous class object directly instead of declaring a named class. The object implements the interface the function expects, while the declaration stays beside the call that needs it. PHP’s anonymous classes documentation describes them as useful for simple, one-off objects.
Pass an anonymous class directly to an interface-typed function
In this example, printFormatted() accepts a Formatter. The argument is constructed at the call site with new class(...) implements Formatter. The class supplies the required format() method, so the function can use it without knowing the object's engine-assigned class name.
<?php
interface Formatter
{
public function format(string $value): string;
}
function printFormatted(Formatter $formatter, string $value): void
{
echo $formatter->format($value), PHP_EOL;
}
printFormatted(
new class('[', ']') implements Formatter {
private string $prefix;
private string $suffix;
public function __construct(string $prefix, string $suffix)
{
$this->prefix = $prefix;
$this->suffix = $suffix;
}
public function format(string $value): string
{
return $this->prefix . $value . $this->suffix;
}
},
'ready'
);
The output is:
[ready]
The practical choice is whether this implementation is genuinely local to the call. Here, the formatting rule is short enough to read where it is used. If the body grows, putting the whole implementation inside an argument can make the call harder to scan. That is a readability judgment, not a restriction on what an anonymous class may contain.
Supply constructor arguments to the one-off object
The brackets in the example come from the arguments to new class('[', ']'). Its constructor stores them, and format() uses those stored values when printFormatted() calls it. PHP anonymous classes can receive constructor arguments as well as implement interfaces and extend classes.
Notice the two different calls involved: the arguments after new class initialize the object, while 'ready' is passed to printFormatted() as the value to format. Keeping those roles separate makes it easier to change the formatting without changing the function's interface. For example, a different pair of constructor strings would change this object's output; the function would still call format() in the same way.
Extend a base class when the object needs its protected method
An anonymous class can also extend a base class. Suppose the shared part of formatting is a protected method. Add the following code after the first example, in the same PHP file:
class BaseFormatter
{
protected function wrap(string $value): string
{
return '<' . $value . '>';
}
}
$formatter = new class extends BaseFormatter implements Formatter {
public function format(string $value): string
{
return $this->wrap($value);
}
};
printFormatted($formatter, 'ready');
echo $formatter instanceof BaseFormatter ? "BaseFormatter: yes\n" : "BaseFormatter: no\n";
That addition prints:
<ready>
BaseFormatter: yes
This variation keeps the one-off implementation of Formatter, but its format() method delegates the wrapping work to the protected method it inherits from BaseFormatter. The instanceof check asks a useful question about that relationship: is this object a BaseFormatter? The displayed result is yes.
Check the interface or base-class type, not the engine-assigned class name
For the first call, the relevant type is Formatter, because that is what printFormatted() requires. For the second object, BaseFormatter is also a meaningful type to check because the anonymous class extends it. PHP assigns anonymous classes names internally, but its manual says those names are implementation details that should not be relied upon. Use the declared interface or base class when you need to reason about what the object can do.
