Jump to content

How to state separately a PW hook that uses the 'use' PHP construct?


LAPS
 Share

Recommended Posts

Hi,

I am a newbie in PHP, and I am learning PW including hooks.

In my ready.php file I have PHP code and a PW hook stated as follow:

$object = ...;

wire()->addHookBefore('Pages::save', function ($event) use (&$object) {
 ...
}

I would like to define a 'hookFunction' separately, something like this:

wire()->addHookBefore('Pages::save', null, 'hookFunction');

However the issue is in stating separately the 'use (&$object)' part.

I know that the 'use' language construct is used for "anonymous functions" and the  '&' is used for "passing by reference", but how can I state the hook function separately?

P.S.: Maybe there is a better way (that I do not know) to state the above, hook-related code at all.

Link to comment
Share on other sites

30 minutes ago, LAPS said:

I would like to define a 'hookFunction' separately, something like this:


wire()->addHookBefore('Pages::save', null, 'hookFunction');

Did this not work? It should.

wire()->addHookBefore('Pages::save', null, 'hookFunction');// grab 

function hookFunction(HookEvent $event) {
// do your stuff
}

http://processwire.com/api/hooks/

Edited by kongondo
Link to comment
Share on other sites

@kongondo,

I tried many ways to state that particular hook separately, including your that does not work. It seems that, even if the 'hookFunction' is called, within the 'hookFunction' the $object is not modified "persistently", maybe because it is not passed by reference. This way, the $object value modified internally to the 'hookFunction' is not modified for use within other, subsequent hooks-functions.

Link to comment
Share on other sites

Not entirely sure about the following, so would be glad to hear from anyone who understands this better. @ryan?

I think you must pass in any external variables to a named hook function as part of the $options array. See this line in the HookEvent class:

@property array $options An optional array of user-specified data that gets sent to the hooked function. The hook handling method may access it from $event->data. Also includes all the default hook properties. 

So you would assign a custom key/value to the $options array by reference.

An example:

$colour = 'red';
$options = array();
$options['colour'] = &$colour; // assign by reference
$wire->addHookAfter('Pages::save', null, 'hookFunction', $options);

function hookFunction(HookEvent $event) {
    $opts = $event->options;
    $opts['colour'] = 'green';
    // $colour is now 'green'
    // ...
}

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...