Version: 1.0
TinyHooks is perhaps the smallest PHP hook library on earth. Still packed with features.
include __DIR__ . '/tinyhooks.php';
The most basic way to setup a hook is with a anonymus function like below.
hook::set('myhook', function($args) {
return 'Hello ' . $args;
});
Below will output Hello world
on the screen, if you have set the hook like above.
echo hook('myhook', 'world');
If you use a string as a second argument, you will call a function.
hook::set($name, 'about');
function about($args) {
// Do something
}
To call a static function you need to include the class name like below.
hook::set($name, 'MyStatic::myhook');
class MyStatic {
public static function myhook($args) {
// Do something
}
}
To use a function in a class, you need to create an object. Then you need to send the object and the class name as an array, like below.
$object = new MyClass();
hook::set($name, [$object, 'myhook']);
class MyClass {
function myhook($args) {
// Do something
}
}
You can setup all your hooks with an array. The key
of every row is the pattern and the value
is the call. That way it works very similar to the hook::set()
function.
hooks::set([
[
'myhook' => function($args) {
// Do something
}
],
[
'myhook' => function($args) {
// Do something
}
]
]);
You can use the hooks both as actions or as filters.
If you want to do something and don't want to return something, it's an action. If you want it to behave more like a filter, then return something from the hook.
Donate to DevoneraAB if you want.
- To keep it dead simple, namespaces are not used.
- In case of collision, you can rename the
hook
class and thehook
function.
- PHP 7
- Kirby CMS hooks Multiple names support idea.
- WordPress hooks Actions and filters.
MIT