Skip to content

Instantly share code, notes, and snippets.

@adamcameron
Created February 1, 2025 12:34
Show Gist options
  • Save adamcameron/4d5d53f0a71f1bd7a0104e51129e92aa to your computer and use it in GitHub Desktop.
Save adamcameron/4d5d53f0a71f1bd7a0104e51129e92aa to your computer and use it in GitHub Desktop.
Decorator interface with _call to handle method decoration
<?php
interface WrapperInterface {
}
class Wrapper implements WrapperInterface {
public function __construct(private readonly Wrapped $wrapped) {}
public function __call(string $name, array $arguments): mixed {
echo "Wrapped: ";
return $this->wrapped->$name(...$arguments);
}
}
class Wrapped implements WrapperInterface {
public function callMe(string $s) {
echo $s . PHP_EOL;
}
}
$o = new Wrapper(new Wrapped());
$o->callMe("Zachary");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment