开发者

Hooks...exactly what they are

开发者 https://www.devze.com 2022-12-22 01:06 出处:网络
I have seen hooks in Kohana PHP framework, and they work as some sort of a callback function triggered by a certain event (Kohana\'s events that is, some sort of method overloading).

I have seen hooks in Kohana PHP framework, and they work as some sort of a callback function triggered by a certain event (Kohana's events that is, some sort of method overloading).

I have seen hooks in Wordpress, and I don't know what they are or how to use them (just saw them yesterday).

I'm looking for events in "non-frameworked" php, I cannot find ones.

Do hooks work only in an "event-based" environment? What are they anyway (in general, not just in PHP)? What are they good for if not in an "event-based" environment开发者_Go百科.


Hooks are, indeed, hooks into an event stack of sorts; a list of values some controller iterates over, and if you have anything registered to that event, the controller can run your custom code. But PHP itself doesn't have anything (useful) like that, so you make it yourself or use the ones you find in your favorite application / system. It's a fairly common way to create a plugin architecture, but can also be used for application control and other things. I've written earlier about my quest for a more universal event and operating set of stack events, including this post here on StackOverflow.

As others have mentioned, PHP is stateless, so where I use them I use them as a simple execution list, and hook every part of my application into it. This way I'm very extensible, and have a basis for a plugin stack as well. (And I'll release it one magical day when I'm bored or retired or just got too much time on my hands, etc.)

You'll find similar stacks and hooks in, for example, WordPress, so a plugin that deals with, say, CSS, will hook itself to the CSS_DEFINITION_EVENT (basically, that part of the WordPress application that writes CSS stuff into the HTML section). This stuff is everywhere. In PHP it only applies (well, mostly) to the limits of the request you get per PHP page (unless you're doing PHP outside the webserver), but all major operating systems, applications, frameworks and systems have some form of event stack. PHP just doesn't have one (seriously) built in.


PHP is stateless, therefore it cannot really have events. They are emulated with manually adding and storing event listeners ( functions to be called ) and then calling said listeners explicitly when something happens in the code. Like a new picture was uploaded or a 404 error occurred.


Are you looking for these?

  • register_shutdown_function
  • set_error_handler
  • set_exception_handler
0

精彩评论

暂无评论...
验证码 换一张
取 消