15

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.

closed as too broad by gnat, user40980, Bart van Ingen Schenau, user53019, ChrisF Jan 6 '14 at 21:05

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • A hook method is a concrete method in an abstract class that can be overridden by subclasses, but it has a default behaviour in the abstract class. – gal007 Apr 11 '17 at 13:13
18

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.

2

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.

  • Thanks Bill,moles looks like a .NET concept , which is totally alien to me, could you please point me to something pythonic or php or something more general. – Rasmus Oct 23 '10 at 3:07
  • Unfortunately I don't use those two enough to claim expertise, but if I am reading it correctly I believe stackoverflow.com/questions/156230/… is a similar idea being discussed but moles can hook a compiled assembly. A miniport driver is another place the hook phrase is used sometimes. My point was more that the "hook" terminology can be used another way sometimes,in older MS tech anyway, perhaps the second use does not apply to python as well. – Bill Oct 23 '10 at 4:28

Not the answer you're looking for? Browse other questions tagged or ask your own question.