No Category Wabler on 04 Jan 2007 09:16 am
You can create a “hook” in PHP for plugins or additional processing, when you dont want something to act normally. This will allow you to write one script and then not have to go back and make the script ridiculously long, and full of if statements for different conditions. You can change the behavior based on conditions, or you can change it for some other reason.
This is also a good way to do minor revisions to your scripts, for testing, and then take them down quickly, without having to upload an new version of the original. You can just delete the hook file off the server.
Another thing that you can use this for, is testing different blocks of code in the same place, for how your users react. You can create a random number, and pull up a different hook based on that number, and then record how your users react.
What you can do is simply add 1 if statement to the file, around the section of code that you want:
Now what you can do is add the code for the hook in the condition for the if else block:
On the condition that the hook file exists, that file will run instead of the regular code. If it doesn’t the regular code runs. Simple!
Write your plugins, and add the hook if statements around all of your code sections that you would like to be able to modify. Name the files in the original code in such a way that is intuitive and easy to remember, or that is based on possible values of a variable.
Just remember the rules for include, require, include_once, and require_once and use the one that fits your code the best. Otherwise, if you drop your hook statement into a loop, you could cause your code to die when it tries to pull in a file more than once.
Note: Prior to PHP 4.0.2, the following applies: require() will always attempt to read the target file, even if the line it’s on never executes. The conditional statement won’t affect require(). However, if the line on which the require() occurs is not executed, neither will any of the code in the target file be executed. Similarly, looping structures do not affect the behaviour of require(). Although the code contained in the target file is still subject to the loop, the require() itself happens only once.