22

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

Sushant Bhargav
  • 349
  • 3
  • 14
tasha-
  • 241
  • 1
  • 2
  • 4
  • 2
    "Hooks" are a common scheme for implementing plugins/extensions. It's a poor mans observer pattern. PHP itself uses "callbacks" for some features, which is roughly a similar use and syntax. As example: http://stackoverflow.com/questions/5127424/how-does-plugin-system-work-wordpress-mybb/5127470#5127470 – mario May 09 '11 at 00:44

5 Answers5

40

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.

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
  • Few languages have actual "hooks" defined as first-class features. In practice hooks are just a form of [dependency injection](https://en.wikipedia.org/wiki/Dependency_injection) which is a programming technique, not a programming language feature per-se, or an implementation of a [design pattern](https://en.wikipedia.org/wiki/Software_design_pattern). – tadman Nov 15 '18 at 19:20
8

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

Codeigniter Hooks

Rahul Gupta
  • 991
  • 4
  • 12
Atticus
  • 6,585
  • 10
  • 35
  • 57
7

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!

Ahmad Baktash Hayeri
  • 5,802
  • 4
  • 30
  • 43
realmag777
  • 2,050
  • 1
  • 24
  • 22
3

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

It makes it easier to work with.

alex
  • 479,566
  • 201
  • 878
  • 984
0

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();



mohammad13
  • 453
  • 3
  • 17