Skip to content

Instantly share code, notes, and snippets.

@kelunik
Created Apr 17, 2019
Embed
What would you like to do?
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