$hooks->addHook() method

Hook a function/method to a hookable method call in this object

Hookable method calls are methods preceded by three underscores. You may also specify a method that doesn't exist already in the class The hook method that you define may be part of a class or a globally scoped function.

If you are hooking a procedural function, you may omit the $toObject and instead just call via: $this->addHook($method, 'function_name'); or $this->addHook($method, 'function_name', $options);

Usage

// basic usage
$string = $hooks->addHook(Wire $object, $method, $toObject);

// usage with all arguments
$string = $hooks->addHook(Wire $object, $method, $toObject, $toMethod = null, array $options = []);

Arguments

NameType(s)Description
objectWire
methodstring, array

Method name to hook into, NOT including the three preceding underscores. May also be Class::Method for same result as using the fromClass option. May also be array OR CSV string of either of the above to add multiple (since 3.0.137).

toObjectobject, null, callable

Object to call $toMethod from, Or null if $toMethod is a function outside of an object, Or function|callable if $toObject is not applicable or function is provided as a closure.

toMethod (optional)string, array

Method from $toObject, or function name to call on a hook event, or $options array.

options (optional)array

See $defaultHookOptions at the beginning of this class. Optional.

Return value

string

A special Hook ID that should be retained if you need to remove the hook later. If the $method argument was a CSV string or array of multiple methods to hook, then CSV string of hook IDs will be returned, and the same CSV string can be used with removeHook() calls. (since 3.0.137).

Exceptions

Method can throw exceptions on error:

  • WireException


$hooks methods and properties

API reference based on ProcessWire core version 3.0.244

Latest news

  • ProcessWire Weekly #559
    The 559th issue of ProcessWire Weekly brings in all the latest news from the ProcessWire community. Modules, sites, and more. Read on!
    Weekly.pw / 25 January 2025
  • ProcessWire 3.0.244 new main/master version
    ProcessWire 3.0.244 is our newest main/master/stable version. It's been more than a year in the making and is packed with tons of new features, issue fixes, optimizations and more. This post covers all the details.
    Blog / 18 January 2025
  • Subscribe to weekly ProcessWire news

“Indeed, if ProcessWire can be considered as a CMS in its own right, it also offers all the advantages of a CMF (Content Management Framework). Unlike other solutions, the programmer is not forced to follow the proposed model and can integrate his/her ways of doing things.” —Guy Verville, Spiria Digital Inc.