Question

When I look at at some PHP applications, they typically have hooks. Drupal and Wordpress are examples. When I look at an application from the .NET world like Orchard or Umbraco, one subscribes to events.

On the other hand, I saw Drupal also has events, https://www.drupal.org/docs/8/modules/simple-fb-connect/eventsubscriber-example

WordPress has hooks only (that I could find), though they do say hooks are hooking into events. https://codex.wordpress.org/Plugin_API#Hook_to_WordPress

It appears to me, when an application with a plugin/module extensibility framework references an Event, they are meaning an Object Oriented Design that uses Dependency Injection, typically with a container that wires it all up. But, if not then it is a program that saves references in an array, using them at an appropriate part of the execution sequence calling the hook. They are close in operation but not implementation.

Are hooks the event subscriptions the same thing? I've also seen them called "Signals."

Was it helpful?

Solution

Well, both of them are ways of implementing polymorphism.

Hooks can actually be placed anywhere. There is a technique where you copy a bit of machine code off somewhere else and in it's place put your own code. You can make this seemless if your code also call's "somehwere else" so you don't loose whatever behavior was in the code you stepped on. It's a hack. It's not really about web apps. But it's where the name comes from.

Rather then force the hack, you can design for this by offering points in the code where you call whatever you've been handed. This is also called hooking but it's also called composition and delegation. It can also be done with inheritance, function pointers, first class functions, virtual tables... These points are called extension points. They are less flexible then the hack because they only work where they have been designed to work. But they are far easier to understand when reading code.

Event Subscriptions use these extension points by putting them into a collection that can be modified at run time. Another name for them is the Observer Pattern.

C# delegates, observers, events, all different names for the same pattern.

Which in a nut shell is: they call your foo() so you call all the foo() in the collection. Let them add and remove foo()s as they like.

It's a pattern that works in OOP, functional programming, and procedural programming. But functional programming will want to get all formal about when you can mutate the collection.

So are they the same? No. They're as different as a banana and a bunch of bananas in a banana shaped box. I can see how they might seem the same.

OTHER TIPS

To me the main difference is that hooks, once connected are actually invoked by the hosting code, i.e. when the main code reaches a point that is hook accessible it explicitly executes the hook code. On the other hand in the publish and subscribe model when the main code reaches the publish point it sends out the appropriate data, (mechanisms vary), and proceeds merrily on its way not caring if anything is listening. Any code that has subscribed that specific event will, when it get notified of the even, (e.g. by the OS), and totally independently react to the event.

As such hooks are synchronous and can return values, modify the data, etc. and will, normally execute in the same thread as the hooked into process but pub-sub is asynchronous, cannot directly return values and would be processed in a different thread, possibly even on a different machine. If there is a need to pass data back from hooks it is simple, assuming that the hook definition allows a return but pub-sub would need to publish its own results, again one way or another, and hope that the original process is listening.

Licensed under: CC-BY-SA with attribution
scroll top