Skip to content

Instantly share code, notes, and snippets.

@kelunik
Created April 17, 2019 16:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kelunik/9f32704c49c0d4b7dcf711645b8a253c to your computer and use it in GitHub Desktop.
Save kelunik/9f32704c49c0d4b7dcf711645b8a253c to your computer and use it in GitHub Desktop.
Short closure return value handling
<?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