Created
February 1, 2025 12:34
-
-
Save adamcameron/4d5d53f0a71f1bd7a0104e51129e92aa to your computer and use it in GitHub Desktop.
Decorator interface with _call to handle method decoration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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