jessegall / php-hooks
This package's canonical repository appears to be gone and the package has been frozen as a result.
Requires (Dev)
- phpunit/phpunit: ^9.5
- symfony/var-dumper: ^6.1
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 } }