---
title: Hooks
---

# Hooks

<div class="documentation__toc"></div>

@TODO remove Laravel links to preserve version integrity

A `hook` is a method of directly adding functionality to a class from the outside and without having to extend it or directly modify it.

## Introduction

A `hook` is similar to a [callback trigger](#services/callbacks/introduction/triggers). A hook applies to any instance of a class and any parent that inherits that class.

## Bindings

By `binding` a hook you can change the context of `$this` so that the hook behaves like an actual method of the class. Where `$this` refers to the class the hook is being run _on_ and not where the hook is being defined (like normal Closure behavior).

## Basic Usage

Use the `\Anomaly\Streams\Platform\Traits\Hookable` trait in your class to get started.

### Hookable::hook()

The `hookable` method let's you register a hook on a hookable class.

#### Returns: `$this`

#### Arguments

<table class="table table-bordered table-striped">

<thead>

<tr>

<th>Key</th>

<th>Required</th>

<th>Type</th>

<th>Default</th>

<th>Description</th>

</tr>

</thead>

<tbody>

<tr>

<td>

$hook

</td>

<td>

true

</td>

<td>

string

</td>

<td>

none

</td>

<td>

The hook name.

</td>

</tr>

<tr>

<td>

$callback

</td>

<td>

true

</td>

<td>

string|Closure

</td>

<td>

none

</td>

<td>

The callback logic or callable string.

</td>

</tr>

</tbody>

</table>

#### Example

    $hookable->hook(
        'avatar',
        function ($email) {
            return 'https://www.gravatar.com/avatar/' . md5($email);
        }
    );

    $hookable->avatar('example@domain.com');

### Hookable::bind()

The `bind` method is very similar to `hook` but the callback is available for all instances of the class as well as any parents of the class.

#### Returns: `$this`

#### Arguments

<table class="table table-bordered table-striped">

<thead>

<tr>

<th>Key</th>

<th>Required</th>

<th>Type</th>

<th>Default</th>

<th>Description</th>

</tr>

</thead>

<tbody>

<tr>

<td>

$hook

</td>

<td>

true

</td>

<td>

string

</td>

<td>

none

</td>

<td>

The hook name.

</td>

</tr>

<tr>

<td>

$callback

</td>

<td>

true

</td>

<td>

string|Closure

</td>

<td>

none

</td>

<td>

The callback logic or callable string.

</td>

</tr>

</tbody>

</table>

#### Example

    $hookable->bind(
        'customer',
        function () {

            /* @var UserModel $this */
            return $this->hasOne(CustomerModel::class, 'user_id');
        }
    );

    $hookable->bind(
        'get_customer',
        function () {

            /* @var UserModel $this */
            return $this->customer()->first();
        }
    );

### Hookable::call()

The `call` method fires the hook.

#### Returns: `mixed`

#### Arguments

<table class="table table-bordered table-striped">

<thead>

<tr>

<th>Key</th>

<th>Required</th>

<th>Type</th>

<th>Default</th>

<th>Description</th>

</tr>

</thead>

<tbody>

<tr>

<td>

$hook

</td>

<td>

true

</td>

<td>

string

</td>

<td>

none

</td>

<td>

The hook to call.

</td>

</tr>

<tr>

<td>

$parameters

</td>

<td>

false

</td>

<td>

array

</td>

<td>

null

</td>

<td>

Parameters to pass to the callback.

</td>

</tr>

</tbody>

</table>

#### Example

    $hookable->call('get_customer')->billing_address;

#### Twig

    {{ user().call('get_customer').billing_address }}

### Hookable::hasHook()

The `hasHook` method returns whether a hook exists or not for the object.

#### Returns: `boolean`

#### Arguments

<table class="table table-bordered table-striped">

<thead>

<tr>

<th>Key</th>

<th>Required</th>

<th>Type</th>

<th>Default</th>

<th>Description</th>

</tr>

</thead>

<tbody>

<tr>

<td>

$hook

</td>

<td>

true

</td>

<td>

string

</td>

<td>

none

</td>

<td>

The hook name to check existance of.

</td>

</tr>

</tbody>

</table>

#### Example

    $hookable->hasHook('get_customer');

### Method Handlers

Not always, but generally classes in the Streams Platform that use the `Hookable` trait will map the `__call` method through hooks.

What this means is that hooks will be checked for when the `__call` method is triggered.

If the `hook` is `get_customer` the method will be `getCustomer`.

    // First bind the hook.
    $hookable->bind(
        'get_customer',
        function () {

            /* @var UserModel $this */
            return $this->customer()->first();
        }
    );

    // Now fire using the handler method.
    $hookable->getCustomer()->billing_address;

### Getter Behavior

Not always, but generally classes in the Streams Platform that use the `Hookable` trait will map the `__get` method through hooks.

What this means is that hooks prefixed with `get_` will be checked for when the `__get` method is triggered.

    // Register the hook
    $hookable->bind(
        'get_customer',
        function () {

            /* @var UserModel $this */
            return $this->customer()->first();
        }
    );

    // Call the hook with method handler.
    $hookable->customer->billing_address;

This method is very helpful in the view layer:

    {{ user().customer.billing_address }} 
