Pergunta

I keep coming across this term hooks in various programming articles. However I don't understand what they are, and how can they be used. So I just wanted to know what is the concept of hooks; if someone could link me to some examples, particularly in the context of web development, it would be great.

Foi útil?

Solução

My answer pertains to WordPress which is written in PHP, but this is a general development mechanic so it shouldn't really matter, despite the fact that you put 'python' in your question title.

One good example of usage of hooks, coincidentally in web development, are WordPress' hooks.

They are named appropriately in that they allow a way to 'hook into' certain points of the execution of a program.

So for example, the wp_head is an 'action' that is emitted when a WordPress theme is being rendered and it's at the part where it renders the part that's within the <head> tags. Say that you want to write a plugin that requires an additional stylesheet, script, or something that would normally go within those tags. You can 'hook into' this action by defining a function to be called when this action is emitted. Something like:

add_action('wp_head', 'your_function');

your_function() could be something as simple as:

function your_function() {
    echo '<link rel="stylesheet" type="text/css" href="lol.css" />';
}

Now, when WordPress emits this action by doing something like do_action('wp_head');, it will see that your_function() was 'hooked into' that action, so it will call that function (and pass it any arguments if it takes any, as defined in the documentation for any particular hook).

Long story short: It allows you to add additional functionality at specific points of the execution of a program by 'hooking into' those points, in most cases by assigning a function callback.

Outras dicas

Depending on the background of the person asking sometimes the answer to this question can be as simple as hooks are usually another word for an event handler, which Blaenk gave an excellent explanation for.

There are also uses of this phrase that are when you inject an event or code into a process that otherwise does not have events like moles for example.

Licenciado em: CC-BY-SA com atribuição
scroll top