Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a hook in PHP?

Tags:

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

like image 232
tasha- Avatar asked May 09 '11 00:05

tasha-


People also ask

Why do we use hook in PHP?

Hooks allow developers to change or extend WordPress' functionality without needing to edit the WordPress core code itself. They do this by running actions and filters, which are PHP functions that perform tasks and make changes to data. They are used extensively by plugin and theme developers.

What is the function of a hook?

Hooks are a category of function that allows base code to call extension code. This can be useful in situations in which a core developer wants to offer extensibility without exposing their code.

What are hooks in code?

In programming, a hook is a place and usually an interface provided in packaged code that allows a programmer to insert customized programming. For example, a programmer might want to provide code that analyzed how often a particular logic path was taken within a program.

How many types of hooks are there in PHP?

There are two types of hooks: Actions and Filters. To use either, you need to write a custom function known as a Callback , and then register it with a WordPress hook for a specific action or filter.


2 Answers

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.

like image 137
BoltClock Avatar answered Dec 08 '22 04:12

BoltClock


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

Codeigniter Hooks

like image 33
Atticus Avatar answered Dec 08 '22 03:12

Atticus