Created
April 17, 2019 16:41
-
-
Save kelunik/9f32704c49c0d4b7dcf711645b8a253c to your computer and use it in GitHub Desktop.
Short closure return value handling
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 | |
class EventManager { | |
private $handlers; | |
public function onEvent(callable $callback) { | |
$this->handlers[] = $callback; | |
} | |
public function invoke(Event $event) { | |
foreach ($this->handlers as $handler) { | |
$returnValue = $handler($event); | |
if ($returnValue === false) { | |
break; | |
} | |
} | |
} | |
} | |
class Cache { | |
public function set(string $key, string $value) { | |
return true; // returns true if value already existed, otherwise false | |
} | |
} | |
$eventManager = new EventManager; | |
$cache = new Cache; | |
$eventManager->onEvent(fn ($e) => $cache->set($e->getName(), $e->getValue()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment