PHPackages                             bayfrontmedia/php-hooks - 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. bayfrontmedia/php-hooks

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

bayfrontmedia/php-hooks
=======================

An easy to use hooks library for managing events and filters.

v2.0.1(1y ago)31.9k1MITPHPPHP ^8.0

Since Aug 3Pushed 1y ago1 watchersCompare

[ Source](https://github.com/bayfrontmedia/php-hooks)[ Packagist](https://packagist.org/packages/bayfrontmedia/php-hooks)[ Docs](https://github.com/bayfrontmedia/php-hooks)[ RSS](/packages/bayfrontmedia-php-hooks/feed)WikiDiscussions master Synced 1w ago

READMEChangelog (4)Dependencies (1)Versions (5)Used By (1)

PHP hooks
---------

[](#php-hooks)

An easy to use hooks library for managing events and filters.

- [License](#license)
- [Author](#author)
- [Requirements](#requirements)
- [Installation](#installation)
- [Usage](#usage)

License
-------

[](#license)

This project is open source and available under the [MIT License](LICENSE).

Author
------

[](#author)

[![Bayfront Media](https://camo.githubusercontent.com/0c0163913b2092e97dbf9684970adaf86f2f25871298d3d28b2dff4bf75b2915/68747470733a2f2f63646e312e6f6e62617966726f6e742e636f6d2f62666d2f6272616e642f62666d2d6c6f676f2e737667)](https://camo.githubusercontent.com/0c0163913b2092e97dbf9684970adaf86f2f25871298d3d28b2dff4bf75b2915/68747470733a2f2f63646e312e6f6e62617966726f6e742e636f6d2f62666d2f6272616e642f62666d2d6c6f676f2e737667)

- [Bayfront Media homepage](https://www.bayfrontmedia.com?utm_source=github&utm_medium=direct)
- [Bayfront Media GitHub](https://github.com/bayfrontmedia)

Requirements
------------

[](#requirements)

- PHP `^8.0` (Tested up to `8.4`)

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

[](#installation)

```
composer require bayfrontmedia/php-hooks

```

Usage
-----

[](#usage)

### Start using hooks

[](#start-using-hooks)

```
use Bayfront\Hooks\Hooks;

$hooks = new Hooks();

```

### Public methods

[](#public-methods)

**Events**

- [addEvent](#addevent)
- [hasEvent](#hasevent)
- [getEvents](#getevents)
- [removeEvent](#removeevent)
- [removeEvents](#removeevents)
- [doEvent](#doevent)

**Filters**

- [addFilter](#addfilter)
- [hasFilter](#hasfilter)
- [getFilters](#getfilters)
- [removeFilter](#removefilter)
- [removeFilters](#removefilters)
- [doFilter](#dofilter)

---

### addEvent

[](#addevent)

**Description:**

Adds a hook for a given event name.

**NOTE:** Anonymous functions are unable to be removed with `removeEvent()`, so use them carefully.

**Parameters:**

- `$name` (string): Name of event
- `$function` (callable)
- `$priority = 10` (int): Hooks will be executed by order of priority in ascending order (lower numbers = earlier execution)

Reserved names:

- `always`: These hooks will always be executed whenever `doEvent()` is called, regardless of the name.
- `destruct`: These hooks will be executed when the script terminates.

**Returns:**

- (void)

**Examples:**

Anonymous function

```
$hooks->addEvent('name', function($name) {

    echo 'My name is ' . $name;

});

```

Named function

```
function my_name($name) {

    echo 'My name is ' . $name;

}

$hooks->addEvent('name', 'my_name');

```

Inside class scope

```
use Bayfront\Hooks\Hooks;

class MyClass {

    protected $hooks;

    public function __construct(Hooks $hooks) {

        $this->hooks = $hooks;

        $this->hooks->addEvent('name', [$this, 'my_name']);

    }

    public function my_name($name) {

        echo 'My name is ' . $name;

    }
}

$my_class = new MyClass($hooks);

```

Use variables from outside scope

```
$prefix = 'My name is ';

$hooks->addEvent('name', function($name) use ($prefix) {

    echo $prefix . $name;

});

```

---

### hasEvent

[](#hasevent)

**Description:**

Checks if any events exist for a given name.

**Parameters:**

- `$name` (string): Name of event

**Returns:**

- (bool)

**Example:**

```
if ($hooks->hasEvent('name')) {
    // Do something
}

```

---

### getEvents

[](#getevents)

**Description:**

Return array of all hooks for all events, or of a given event name.

**Parameters:**

- `$name = NULL` (string|null): Name of event

**Returns:**

- (array)

**Example:**

```
print_r($hooks->getEvents()); // Returns all hooks for all events

print_r($hooks->getEvents('name')); // Returns all hooks for "name" event

```

---

### removeEvent

[](#removeevent)

**Description:**

Removes hook from a given event, if existing.

**NOTE:** Hooks using anonymous functions cannot be removed using this method.

**Parameters:**

- `$name` (string): Name of event
- `$function` (callable): Hook to remove

**Returns:**

- (bool): Returns `true` if the hook existed

**Example:**

```
$hooks->removeEvent('name', 'my_name');

```

To remove a hook for a function from within a class scope, the `$function` parameter must be an array whose first value is an instance of the class, and second value is the name of the function within the class:

```
$hooks->removeEvent('name', [$my_class, 'my_name']);

```

---

### removeEvents

[](#removeevents)

**Description:**

Removes all hooks from a given event, if existing.

**Parameters:**

- `$name` (string): Name of event

**Returns:**

- (bool): Returns `true` if the hook existed

**Example:**

```
$hooks->removeEvents('name');

```

---

### doEvent

[](#doevent)

**Description:**

Execute queued hooks for a given event in order of priority.

**Parameters:**

- `$name` (string): Name of event
- `...$arg` (mixed): Optional additional argument(s) to be passed to the functions hooked to the event

**Returns:**

- (void)

**Example:**

```
$hooks->doEvent('name', 'John');

```

---

### addFilter

[](#addfilter)

**Description:**

Adds a hook for a given filter name.

**Parameters:**

- `$name` (string): Name of filter
- `$function` (callable)
- `$priority = 10` (int): Filters will be executed in order of priority in ascending order (lower numbers = earlier execution)

**Returns:**

- (void)

**Examples:**

Anonymous function

```
$hooks->addFilter('name', function($name) {

    return strtoupper($name);

});

```

Named function

```
function uppercase($name) {

    return strtoupper($name);

}

$hooks->addFilter('name', 'uppercase');

```

Inside class scope

```
use Bayfront\Hooks\Hooks;

class MyClass {

    protected $hooks;

    public function __construct(Hooks $hooks) {

        $this->hooks = $hooks;

        $this->hooks->addFilter('name', [$this, 'uppercase']);

    }

    public function uppercase($name) {

        return strtoupper($name);

    }
}

$my_class = new MyClass($hooks);

```

Use variables from outside scope

```
$prefix = 'My name is ';

$hooks->addFilter('name', function($name) use ($prefix) {

    return strtoupper($prefix . $name);

});

```

---

### hasFilter

[](#hasfilter)

**Description:**

Checks if any filters exist for a given name.

**Parameters:**

- `$name` (string): Name of filter

**Returns:**

- (bool)

**Example:**

```
if ($hooks->hasFilter('name')) {
    // Do something
}

```

---

### getFilters

[](#getfilters)

**Description:**

Return array of all hooks for all filters, or of a given filter name.

**Parameters:**

- `$name = NULL` (string|null): Name of filter

**Returns:**

- (array)

**Example:**

```
print_r($hooks->getFilters()); // Returns all hooks for all filters

print_r($hooks->getFilters('name')); // Returns all hooks for "name" filter

```

---

### removeFilter

[](#removefilter)

**Description:**

Removes hook from a given filter, if existing.

**NOTE:** Hooks using anonymous functions cannot be removed using this method

**Parameters:**

- `$name` (string): Name of filter
- `$function` (callable): Hook to remove

**Returns:**

- (bool): Returns `true` if the hook existed

**Example:**

```
$hooks->removeFilter('name', 'uppercase');

```

To remove a hook for a function from within a class scope, the `$function` parameter must be an array whose first value is an instance of the class, and second value is the name of the function within the class:

```
$hooks->removeFilter('name', [$my_class, 'uppercase']);

```

---

### removeFilters

[](#removefilters)

**Description:**

Removes all hooks from a given filter, if existing.

**Parameters:**

- `$name` (string): Name of filter

**Returns:**

- (bool): Returns `true` if the hook existed

**Example:**

```
$hooks->removeFilters('name');

```

---

### doFilter

[](#dofilter)

**Description:**

Filters value through queued filters in order of priority.

**Parameters:**

- `$name` (string): Name of filter
- `$value` (mixed): Original value to be filtered

**Returns:**

- (mixed): Filtered value

**Example:**

```
echo $hooks->doFilter('name', 'John');

```

###  Health Score

36

—

LowBetter than 82% of packages

Maintenance40

Moderate activity, may be stable

Popularity20

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity63

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 ~534 days

Total

4

Last Release

509d ago

Major Versions

v1.1.0 → v2.0.02023-01-28

PHP version history (2 changes)1.0.0PHP &gt;=7.1.0

v2.0.0PHP ^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/5ad62c8d0e69358fd63b16fdaa71d5359231cd0cf660bbc3419071dc705c63a8?d=identicon)[bayfrontmedia](/maintainers/bayfrontmedia)

---

Top Contributors

[![robinsonjohn](https://avatars.githubusercontent.com/u/24327848?v=4)](https://github.com/robinsonjohn "robinsonjohn (9 commits)")

---

Tags

actioneventfilterhooksphpphpeventfilterhooksaction

### Embed Badge

![Health badge](/badges/bayfrontmedia-php-hooks/health.svg)

```
[![Health](https://phpackages.com/badges/bayfrontmedia-php-hooks/health.svg)](https://phpackages.com/packages/bayfrontmedia-php-hooks)
```

###  Alternatives

[tormjens/eventy

The WordPress filter/action system in Laravel

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

A collection of Git PHP Hooks

1210.1k3](/packages/wcm-git-php-hooks-library)

PHPackages © 2026

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