jessegall/php-hooks

v0.1.0 2022-12-26 23:19 UTC

This package is auto-updated.

Last update: 2023-06-27 19:58:54 UTC


README

A simple package that makes it easy to hook traits to any class.

Installation

composer require jessegall/php-hooks

Usage

To enable hooks in your class, you will need to use the HasHooks trait and add it to your class:

use JesseGall\Hooks\HasHooks;

class MyClass
{
    use HasHooks;
}

You can then define hook methods in your traits by adding methods with a specific naming convention. The method name should be prefixed with hook, followed by the name of the trait. For example:

trait MyTrait
{
    public function hookMyTrait()
    {
        // Will be called when the hook is initialized
    }  
}

Of course, you'll also need to add the trait to your class:

use JesseGall\Hooks\HasHooks;
use MyTrait;

class MyClass
{
    use HasHooks, MyTrait;
}

To initialize the hooks, you can call the initializeHooks method on an instance of your class:

$myClass = new MyClass();
$myClass->initializeHooks();

Customizing Hooks

By default, the hook methods are expected to be prefixed with hook. You can change this prefix by defining a hookPrefix property in your class:

use JesseGall\Hooks\HasHooks;

class MyClass
{
    use HasHooks, MyTrait;

    protected $hookPrefix = 'myHook';
}

With the above configuration, the hook methods in your traits should be prefixed with myHook instead of hook. For example:

trait MyTrait
{
    public function myHookMyTrait()
    {
        // This method will be called when the hook is initialized
    }  
}