tasha-
tasha-

Reputation: 241

What is a hook in PHP?

I tried to search on Google but couldn't find any good tutorial or article.

Upvotes: 22

Views: 56019

Answers (5)

mohammad13
mohammad13

Reputation: 463

you can fallow this codes


<?php


 $action = [];

 function apply($hook, $args){
    global $action;
    $action[$hook]['args'] = $args;
    return doa($hook, $args);
 }

 function add($hook, $func){
    global $action;
    $action[$hook]['funcs'][] = $func;
 }

 function doa($hook,$args){
    global $action;
    if(isset($action[$hook]['funcs'])){
        foreach($action[$hook]['funcs'] as $k => $func){
            call_user_func_array($func, $args);
       }
    }
    
 }

 add('this_is', 'addOne');
function addOne($user){
    echo "this is test add one $user <br>";
}
add('this_is', function(){
    echo 'this is test add two <br>';
});


add('this_is_2', 'addTwo');
function addTwo($user, $name){
    echo $user . '   ' . $name . '<br>';
}


function test(){
    echo 'hello one <br>';
    apply('this_is', ['user'=> 123]);
}


function test2(){
    echo 'hello two <br>';
    apply('this_is_2', ['user'=> 123, 'name' => 'mohammad']);
}
test();
test2();



Upvotes: 0

Atticus
Atticus

Reputation: 6720

Yeah, hooks aren't native PHP methods.. they're used to extend functionality from a framework's core.

Codeigniter Hooks

Upvotes: 8

realmag777
realmag777

Reputation: 2088

You can emulate hooks in your own PHP project:

1) Create and include next class:

class Hooks {

    private static $actions = array(
        'ev_after_user_create' => array(),
        'ev_after_user_profile_update' => array()
    );

    public static function apply($hook, $args = array()) {
        if (!empty(self::$actions[$hook])) {
            foreach (self::$actions[$hook] as $f) {
                $f($args);
            }
        }
    }

    public static function add_action($hook, $function) {
        self::$actions[$hook][] = $function;
    }

}

Define there name of hooks you prefer.

2) Now you can use hooks in you code, for example for do smth after new user created (example):

//here is going any code which creates new user  
//hooks
Hooks::apply('ev_after_user_create', array('user_id' => $new_user_id));

3) Define hooks actions in the next manner:

Hooks::add_action('ev_after_user_create', function($args) {
    if (Router::$application === 'front') {
        require_model('users-data');
        $ud = new MUsersData(8);
        $ud->update_data($ud->create_page(), $args, 'id');
    }
});

Any hooks actions code should be defined BEFORE code where its action is needed!

Upvotes: 7

alex
alex

Reputation: 490283

You can implement the observer pattern with some of the new SPL stuff, such as SplObserver().

It makes it easier to work with.

Upvotes: 3

BoltClock
BoltClock

Reputation: 723729

You probably couldn't find anything because PHP doesn't have a concept of hooks in the first place.

Hooks are a kind of function which you can plug (or hook) to an existing system to extend its functionality. They aren't specific to the PHP language or to any system. They may also be called plugins, add-ons or extensions.

Now, while PHP doesn't have a concept of hooks, it does allow you to compile extensions together with the PHP core to gain added functionality for use in your scripts. There are plenty of PHP extensions bundled by default. This is an example of what I described above.

Upvotes: 40

Related Questions