PHPackages                             sciactive/hookphp - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. sciactive/hookphp

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

sciactive/hookphp
=================

Method hooking in PHP.

2.1.0(6y ago)333.0k7[5 issues](https://github.com/sciactive/hookphp/issues)3Apache-2.0PHPPHP ~7.0CI failing

Since Dec 15Pushed 6y ago4 watchersCompare

[ Source](https://github.com/sciactive/hookphp)[ Packagist](https://packagist.org/packages/sciactive/hookphp)[ Docs](http://hookphp.org/)[ RSS](/packages/sciactive-hookphp/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (9)Dependencies (1)Versions (10)Used By (3)

HookPHP
=======

[](#hookphp)

[![Build Status](https://camo.githubusercontent.com/6a0d61200e3e8f2b6d80348049c16c79df7bb1bc49a0b912f04da4e15f2c0119/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f7363696163746976652f686f6f6b7068702e737667)](https://travis-ci.org/sciactive/hookphp) [![Latest Stable Version](https://camo.githubusercontent.com/6df4efd7793a492c1772ac0d9b3c4ec09d175ed96573999ea927e5ff5c84327f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7363696163746976652f686f6f6b7068702e7376673f7374796c653d666c6174)](https://packagist.org/packages/sciactive/hookphp) [![License](https://camo.githubusercontent.com/64cbcec0cef9637e91f9dc8ac1d224db74bf03f8def4c21b4de93ff9f8e7b2fd/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f7363696163746976652f686f6f6b7068702e7376673f7374796c653d666c6174)](https://packagist.org/packages/sciactive/hookphp) [![Open Issues](https://camo.githubusercontent.com/6867fb876ccfd4aa75ffe9479b4805899828037dacb25b1660439ac5e583d3c7/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6973737565732f7363696163746976652f686f6f6b7068702e7376673f7374796c653d666c6174)](https://github.com/sciactive/hookphp/issues) [![Code Quality](https://camo.githubusercontent.com/d764c78e7d1ef020732149537d85fce553cd888bcfd0956848b5d8c9869e5ee4/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f7363696163746976652f686f6f6b7068702e7376673f7374796c653d666c6174)](https://scrutinizer-ci.com/g/sciactive/hookphp/) [![Coverage](https://camo.githubusercontent.com/9466e9b3e249e428591dcfb20c21c919eff517f3644c55926dfdc1a721843a3c/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f636f7665726167652f672f7363696163746976652f686f6f6b7068702e7376673f7374796c653d666c6174)](https://scrutinizer-ci.com/g/sciactive/hookphp/)

Method hooking (decorators) in PHP.

Installation
------------

[](#installation)

You can install HookPHP with Composer.

```
composer require sciactive/hookphp
```

Getting Started
---------------

[](#getting-started)

If you don't use an autoloader, all you need to do is include the Hook.php file.

```
require("Hook.php");
```

Now you can start setting up objects for method hooking.

```
class Test {
  function testFunction($string) {
    echo $string;
  }
}
$obj = new Test();
\SciActive\Hook::hookObject($obj, 'Test->');
```

And modifying their method calls.

```
\SciActive\Hook::addCallback('Test->testFunction', -2, function(&$arguments, $name, &$object, &$function, &$data){
  $arguments[0] = 'New argument instead.';
});
```

And now calling a hooked method like this:

```
$obj->testFunction("This won't print.");
```

Will output this:

```
New argument instead.

```

Adding a Callback with addCallback
----------------------------------

[](#adding-a-callback-with-addcallback)

A callback is called either before a method runs or after. The callback is passed an array of arguments or return value which it can freely manipulate. If the callback runs before the method and sets the arguments array to false (or causes an error), the method will not be run. Callbacks before a method are passed the arguments given when the method was called, while callbacks after a method are passed the return value (in an array) of that method.

The callback can receive up to 5 arguments, in this order:

- `&$arguments` - An array of either arguments or a return value.
- `$name` - The name of the hook.
- `&$object` - The object on which the hook caught a method call.
- `&$function` - A callback for the method call which was caught. Altering this will cause a different function/method to run.
- `&$data` - An array in which callbacks can store data to communicate with later callbacks.

A hook is the name of whatever method it should catch. A hook can also have an arbitrary name, but be wary that it may already exist and it may result in your callback being falsely called. In order to reduce the chance of this, always use namespaced names to begin arbitrary hook names. E.g. '\\MyProject\\PlayerBonus'.

If the hook is called explicitly, callbacks defined to run before the hook will run immediately followed by callbacks defined to run after.

A negative $order value means the callback will be run before the method, while a positive value means it will be run after. The smaller the order number, the sooner the callback will be run. You can think of the order value as a timeline of callbacks, zero (0) being the actual method being hooked.

Additional identical callbacks can be added in order to have a callback called multiple times for one hook.

The hook "all" is a pseudo hook which will run regardless of what was actually caught. Callbacks attached to the "all" hook will run before callbacks attached to the actual hook.

Note: Be careful to avoid recursive callbacks, as they may result in an infinite loop.

The parameters and return value for `addCallback` are:

- @param string $hook The name of the hook to catch.
- @param int $order The order can be negative, which will run before the method, or positive, which will run after the method. It cannot be zero.
- @param callback The callback.
- @return array An array containing the IDs of the new callback and all matching callbacks.

Options for a Callback
----------------------

[](#options-for-a-callback)

A callback is passed the following parameters:

- `&$arguments` - An array of either arguments or a return value.
- `$name` - The name of the hook.
- `&$object` - The object on which the hook caught a method call.
- `&$function` - A callback for the method call which was caught. Altering this will cause a different function/method to run.
- `&$data` - An array in which callbacks can store data to communicate with later callbacks.

A callback can alter `$arguments` to alter what is given to or returned from the method call.

A callback can replace or alter `$object` to affect what the method is being called on.

A callback can replace `$function` to cause a different function or method to run instead.

A callback can add and alter data in the `$data` array to communicate with other callbacks. This is especially useful when communicating with callbacks that run on the other side of the method call.

Retrieving a Hooked Object
--------------------------

[](#retrieving-a-hooked-object)

If you need to retrieve an object that has been hooked, you can use the `_hookObject` method:

```
$originalObject = $hookedObject->_hookObject();
```

Contacting the Developer
------------------------

[](#contacting-the-developer)

There are several ways to contact HookPHP's developer with your questions, concerns, comments, bug reports, or feature requests.

- HookPHP is part of [SciActive on Twitter](http://twitter.com/SciActive).
- Bug reports, questions, and feature requests can be filed at the [issues page](https://github.com/sciactive/hookphp/issues).
- You can directly [email Hunter Perrin](mailto:hunter@sciactive.com), the creator of HookPHP.

###  Health Score

35

—

LowBetter than 80% of packages

Maintenance17

Infrequent updates — may be unmaintained

Popularity31

Limited adoption so far

Community17

Small or concentrated contributor base

Maturity64

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~230 days

Recently: every ~431 days

Total

9

Last Release

2334d ago

Major Versions

1.2.2 → 2.0.02017-06-17

### Community

Maintainers

![](https://www.gravatar.com/avatar/133c9c4eff1268723fc1be664b5312a40527c6a94bfd40215bf4ff276832738f?d=identicon)[hperrin](/maintainers/hperrin)

---

Top Contributors

[![hperrin](https://avatars.githubusercontent.com/u/195918?v=4)](https://github.com/hperrin "hperrin (27 commits)")

---

Tags

interceptionHOOKmethod hooking

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/sciactive-hookphp/health.svg)

```
[![Health](https://phpackages.com/badges/sciactive-hookphp/health.svg)](https://phpackages.com/packages/sciactive-hookphp)
```

###  Alternatives

[tormjens/eventy

The WordPress filter/action system in Laravel

438912.9k16](/packages/tormjens-eventy)[bruli/php-git-hooks

Git hooks for PHP projects.

675370.8k5](/packages/bruli-php-git-hooks)[ramsey/conventional-commits

A PHP library for creating and validating commit messages according to the Conventional Commits specification. Includes a CaptainHook action!

1931.2M122](/packages/ramsey-conventional-commits)[jbzoo/event

Library for event-based development

29760.0k5](/packages/jbzoo-event)[nilportugues/php_backslasher

Adds all PHP internal functions to its namespace by adding backslash to them. Improves the application's performance when OPCache is on.

889.3k18](/packages/nilportugues-php-backslasher)[amphibee/laravel-pint-pre-commit

Laravel Pint pre-commit hook allow you to analyse your code with the awesome Laravel Pint.

1943.9k](/packages/amphibee-laravel-pint-pre-commit)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
