Created
January 19, 2022 20:42
-
-
Save stefanofago73/cc0d0ae6c36a97e77ba3aa86f5307f2a to your computer and use it in GitHub Desktop.
This file contains 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 declare(strict_types = 1); | |
class Logger | |
{ | |
function info(string $msg):void{} | |
} | |
$logger = new Logger(); | |
/** | |
* @return \Closure(array{0:object,1:string}):callable | |
* | |
**/ | |
function decorateWith(Logger $logger):\Closure | |
{ | |
/** @return callable **/ | |
return function(array $call) use($logger) | |
{ | |
$msg = 'object:'.var_export($call[0],true).' invoking: '.$call[1]; | |
$logger->info($msg); | |
/** @var callable $result **/ $result = $call; | |
return $result; | |
}; | |
} | |
class Pippo | |
{ | |
function hello(string $name):void | |
{ | |
echo "HELLO ".$name.PHP_EOL; | |
} | |
} | |
class Pluto | |
{ | |
function execute(callable $task, string $data):void | |
{ | |
$task( $data ); | |
} | |
} | |
$pippo = new Pippo(); | |
$pluto = new Pluto(); | |
$pluto->execute( | |
decorateWith($logger) | |
([$pippo,'hello']),"Stefano"); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment