Library for handling hooks.

1.1.0 2018-01-06 11:28 UTC

This package is auto-updated.

Last update: 2022-06-22 17:05:24 UTC


Latest Stable Version Latest Unstable Version License Codacy Badge Total Downloads Travis PSR2 PSR4 CodeCov

Versión en español

Library for handling hooks.


This library is supported by PHP versions 5.6 or higher and is compatible with HHVM versions 3.0 or higher.


The preferred way to install this extension is through Composer.

To install PHP Hook library, simply:

$ composer require Josantonius/Hook

The previous command will only install the necessary files, if you prefer to download the entire source code you can use:

$ composer require Josantonius/Hook --prefer-source

You can also clone the complete repository with Git:

$ git clone

Or install it manually:

Download Hook.php:

$ wget

Available Methods

Available methods in this library:

- Get Hook instance:

Attribute Description Type Required Default
$id Unique ID for multiple instances. string No '0'

# Return (object) → Hook instance

- Set method name for use singleton pattern:

Attribute Description Type Required Default
$method Set method name for use singleton pattern. callable No

# Return (void)

- Attach custom function to action hook:

Hook::addAction($tag, $function, $priority, $args);
Attribute Description Type Required Default
$tag Action hook name. string Yes
$function Function to attach to action hook. callable Yes
$priority Order in which the action is executed. int No 8
$args Number of arguments accepted. int No 0

# Return (boolean)

- Add actions hooks from array:

Attribute Description Type Required Default
$actions Actions hooks array Yes

# Return (boolean)

- Run all hooks attached to the hook:

By default it will look for getInstance method to use singleton pattern and create a single instance of the class. If it does not exist it will create a new object.

Hook::doAction($tag, $args, $remove);
Attribute Description Type Required Default
$tag Action hook name string Yes
$args Optional arguments mixed No array()
$remove Delete hook after executing actions boolean No true

# Return (mixed|false) → output of the last action or false

- Returns the current action hook:


# Return (string|false) → current action hook

- Check if there is a certain action hook:

Attribute Description Type Required Default
$tag Action hook name string Yes

# Return (boolean)

Quick Start

To use this library with Composer:

require __DIR__ . '/vendor/autoload.php';

use Josantonius\Hook\Hook;

Or If you installed it manually, use it:

require_once __DIR__ . '/Hook.php';

use Josantonius\Hook\Hook;


Example of use for this library:

- Add action hook:

Hook::addAction('css', ['Josantonius\Hook\Test\Example', 'css']);

- Add action hook with priority:

Hook::addAction('js', ['Josantonius\Hook\Test\Example', 'js'], 1);

- Add action hook with priority and arguments number:

$instance = new Example;

Hook::addAction('meta', [$instance, 'meta'], 2, 1);

- Add action hook and set singleton method:


$instance = call_user_func(

Hook::addAction('article', [$instance, 'article'], 3, 0);

- Add multiple action hooks:

$instance = new Example;
    ['after-body', [$instance, 'afterBody'], 4, 0],
    ['footer', [$instance, 'footer'], 5, 0],

- Add multiple action hooks and set singleton method:


$instance = call_user_func(

    ['slide', [$instance, 'slide'], 6, 0],
    ['form', [$instance, 'form'], 7, 2],

- Check if is action:



- Execute action hooks:


- Execute action hook with arguments:

Hook::doAction('meta', 'The title');
Hook::doAction('form', ['input', 'select']);


To run tests you just need composer and to execute the following:

$ git clone

$ cd PHP-Hook

$ composer install

Run unit tests with PHPUnit:

$ composer phpunit

Run PSR2 code standard tests with PHPCS:

$ composer phpcs

Run PHP Mess Detector tests to detect inconsistencies in code style:

$ composer phpmd

Run all previous tests:

$ composer tests


  • Add new feature.
  • Improve tests.
  • Improve documentation.
  • Refactor code for disabled code style rules. See phpmd.xml and .php_cs.dist.


If you would like to help, please take a look at the list of issues or the To Do checklist.

Pull requests

  • Fork and clone.
  • Run the command composer install to install the dependencies. This will also install the dev dependencies.
  • Run the command composer fix to excute code standard fixers.
  • Run the tests.
  • Create a branch, commit, push and send me a pull request.


The file structure from this repository was created with PHP-Skeleton.


This project is licensed under MIT license. See the LICENSE file for more info.


2017 - 2018 Josantonius,

If you find it useful, let me know 😉

You can contact me on Twitter or through my email.