0

I've not that new to php but am trying to figure out how exactly this piece of code reads.

$actions = array();  

function add_action( $hook, $function )  
{  
    global $actions;  

    // create an array of function handlers if it doesn't already exist  
    if( !isset( $actions[ $hook ] ) )  
        $actions[ $hook ] = array();  

    // append the current function to the list of function handlers  
    $actions[ $hook ][] = $function;  
}  

here is my understanding but I believe I'm completely wrong - variable actions is given an array with no params, then a function is created and actions is made into a global giving it global scope. Then there a condition the bit I don't understand that somehow checks for a parem. $actions[ $hook ] its what this bit means that confuses me. is it calling into the array? and why are blank arrays set to variables?

Thanks for clearing this up for me

3 Answers 3

1

First we are declaring a php variable $actions within the global scope:

$actions = array();

Then in the function, you use the global keyword to declare that you are using an already defined global value instead of creating a new value within the scope of the function:

global $actions;

Next, it is using the isset() function to check if the value $hook exists as an index of our global $actions. If the index does not exist, it creates that index and assigns it an empty array as the value.

// create an array of function handlers if it doesn't already exist  
if( !isset( $actions[ $hook ] ) )  
    $actions[ $hook ] = array();

Last, it is using the bracket operator on the array it was just given to append a new value to it, in this case it is giving it the value of $function.

// append the current function to the list of function handlers  
$actions[ $hook ][] = $function;  

So in the end, you have the global $actions variable with an index of $hook which has an array value, and that array value has an index added to it with the value of $function.

Sign up to request clarification or add additional context in comments.

2 Comments

What would be the point of using an empty array then, I guess this is my only question from here.
The point is more of a declaration that the value is an array. If you tried to use the bracket [] operator on an undefined value then you will get an error because it has no idea what that's supposed to do. But if it knows it is an array then it can use operators from one.
1

global $actions doesn't make the variable a global, it was already a global simply because it was created outside any function. This declaration allows the function to access the variable; normally, a function can only access variables that are created within the function.

$actions is a 2-dimensional array. The first dimension is an associative array, keyed off hook names. The second dimension is a linear array of functions that are associated with that hook. The likely use of this is that when a hook is triggered, all the associated functions will be run.

if (!isset($action[$hook])) checks whether there's already an entry in the $actions array with the key $hook. If there isn't, a new entry is created containing an empty array.

$actions[$hook][] = $function then adds an element to the array of actions for that hook.

To see how this works, I suggest you run a simple script that calls add_action() repeatedly, and calls print_r($actions) occasionally:

print_r($actions);
add_action('hook1', 'func1');
print_r($actions);
add_action('hook2', 'func2');
print_r($actions);
add_action('hook1', 'func3');
add_action('hook3', 'func4');
add_action('hook2', 'func5');
print_r($actions);

Comments

0

Blow-By-Blow Walkthrough

The code you posted creates an empty array called $actions. Inside of the function it then uses the global statement to give it access to that array because it was declared outside of the function. This is necessary because otherwise the function would reset the array on every run if it was declared inside it.

The conditional (if) checks to see if the variable $hook is a key of the array and if not, creates an array element the name of which is the value of $hook and initializes it to an empty array.

It then adds the value of the variable $function as an element of the array that either already exists or was just created.

Analysis

This code is designed to create a list of functions that are called at specific "hook" points in code at which those functions are called, here is an example implementation:

add_action('start', 'foo');
add_action('start', 'bar');
add_action('end', 'baz');

Then at various points in code execution, there are designated points at which it checks for actions to run, for example at the very beginning of the code:

foreach($actions['start'] as $function) {
    $function();
}

The above would call foo() and bar(). Then at the end of the code you can repeat the process to call any hooks there (baz).

foreach($actions['end'] as $function) {
    $function();
}

The above would call baz().

2 Comments

foreach($actions['start'] as $function){$function();} doesn't work because the type of $function is an array, you still need to index into it to get to the actual function: $funciton[0]();
@Lochemage No it wouldn't, $actions is a 2 dimensional array as specified in the original code. Because we are specifying the first key as the hook point and iterating over the second key with the foreach, the resulting variable ($function) is the string passed to the original function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.