1. Go to this page and download the library: Download jrbarnard/hookable library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
jrbarnard / hookable example snippets
namespace App;
use JRBarnard\Hookable\Hookable;
class ToBeHooked
{
use Hookable;
public function methodToBeHooked()
{
$data = ['item', 'another_item'];
$someOtherVariable = 'Hello';
// We run a hook by key and pass through some data
$this->runHook('hook_key', $data, $someOtherVariable);
// Because we pass by reference, this variable may have been changed
return $someOtherVariable;
}
}
$toBeHooked = new ToBeHooked();
// Before we have registered the hook
$return = $toBeHooked->methodToBeHooked();
// $return === 'Hello';
// Registering a hook on the key is as simple as specifying the key and the relevant callback.
$toBeHooked->registerHook('hook_key', function($data, &$someOtherVariable){
if ($someOtherVariable === 'Hello') {
$someOtherVariable = 'world';
}
});
// After registering the hook
$return = $toBeHooked->methodToBeHooked();
// $return === 'world';
use JRBarnard\Hookable\Hookable;
abstract class SomeClass
{
use Hookable;
public function store(array $data)
{
$model = new Model();
$model->fill($data);
$result = $model->save($result);
$this->runHook('after_store', $data, $model);
return $result;
}
}
class ChildClass extends SomeClass
{
public function __construct()
{
$this->registerHook('after_store', function(array $data, Model $model){
if ($model->isSpecial()) {
// Do some sort of specific action for this child.
// Perhaps send an email, log something etc.
}
});
// Same hook, will still run, but this will run after, unless we specify priorities.
$this->registerHook('after_store', function(array $data, Model $model){
if ($model->isSpecial()) {
// Do some sort of other action for this child.
}
});
}
}
class SomeClass
{
use Hookable;
}
$someObject = new SomeClass();
$someObject->registerHook('some_hook', function(){ // Do something })
// You can also run hooks externally too (useful for testing, not sure if useful elsewhere)
$someObject->runHook('some_hook');
// Standalone function
function someFunction(){
// Do something
}
// Within a hookable classes constructor
$this->registerHook('hook_key', ['someFunction']);
// Class method
class SomeClass {
use Hookable;
public function someMethod(){
// Do something
}
}
$someObject = new SomeClass();
// Within a hookable classes constructor
$this->registerHook('hook_key', ['someMethod', $someObject]);
class SomeClass {
use Hookable;
public function someMethod($array) {
$this->runHook('hook_key', $array);
return $array;
}
}
$someObject = new SomeClass();
// Calling someMethod now will return the standard array
$array = $someObject->someMethod(['test']);
// $array will equal ['test']
$someObject->registerHook('hook_key', function(&$array){
$array = [];
});
// Calling someMethod now will return the altered array as we have a hook
$array = $someObject->someMethod(['test']);
// $array will equal [];
class SomeClass {
use Hookable;
}
$someObject = new SomeClass();
$someObject->registerHook('test_priority', function(){ echo 'this will appear last'; }, 99);
$someObject->registerHook('test_priority', function(){ echo 'this will appear first'; }, 1);
$someObject->registerHook('test_priority', function(){ echo 'this will appear in the middle'; }, 50);
// We would expect the output:
// this will appear first
// this will appear in the middle
// this will appear last
class SomeClass {
use Hookable;
}
$someObject = new SomeClass();
// Will be false
$someObject->hasHook('test_has_hook');
$someObject->registerHook('test_has_hook', function(){ // Do something });
// Will be true
$someObject->hasHook('test_has_hook');
class SomeClass {
use Hookable;
}
$someObject = new SomeClass();
$someObject->registerHook('hook_one', function(){ // Do something });
$someObject->registerHook('hook_two', function(){ // Do something });
// This will remove all hooks registered to the name hook_one
$someObject->clearHooks('hook_one');
// This will remove all hooks from the object, under all names
$someObject->clearHooks();
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.