4.21 The declare() function and ticks: declare(), register_tick_function(), and unregister_tick_function()This is NOT the latest copy of this book; click here for the latest version.
void register_tick_function ( callback function [, mixed arguments])
void unregister_tick_function ( string function_name)
There is one last set of function calls you need to be aware of, and the first of them is not really a function: declare(). I say "it is not really a function" because it is technically a language construct, and therefore behaves unusually. In fact, declare() is so wholly unlike anything else in PHP that you will not have to use it very often - the most common use is to handle process control.
Put simply, a tick is a special event that occurs internally in PHP each time it has executed a certain number of statements. These statements are internal to PHP and loosely correspond to the statements in your script. You can control how many statements it takes to set off a tick using the declare() function, and you can register functions to execute when a tick occurs by using the register_tick_function() function. As mentioned already, the syntax for declare is very unusual, so be ready for a shock!
Here is an example of these two in action:
<?php
function myfunc() {
print "In tick func\n";
}
register_tick_function("myfunc");
declare(ticks=10) {
for($i = 0; $i < 20; ++$i) {
print "Hello\n";
}
}
declare(ticks=4) {
for($i = 0; $i < 20; ++$i) {
print "Hello\n";
}
} ?>
Firstly, note that the function myfunc() is used as the tick function, which means myfunc() will be executed each time a tick occurs. Now, notice the first use of declare() - "ticks" is set to 10 and a code block is opened, which means that until the matching brace is reached and the block is closed, ticks will occur every ten statements. Inside the declare() statement is a loop that prints out "Hello" twenty times, so you might think myfunc() will be called just twice because twenty divided by ten is two. However, myfunc() will in fact be called four times because the loop iterator is an internal statement also, which brings the total number of statements up to 40.
Immediately after the first declare() block there is a second declare() block where ticks is set to 4 rather than 10, which in turn makes the tick functions execute more often. Although it's fairly rare that you would use two different tick values in one script, it is important to at least know that it can be done.
The declare() "function" can also be used on a global scope, which means you set it once and can forget about it. This is a better choice if you do not plan on having multiple ticks values, as you can just set it and forget it.
To set ticks globally, use declare() like this:
declare(ticks=100);
You can call it multiple times in one script, however if you plan to do so you will likely find it better to use the block notation as it makes the tick setting more obvious. You can also register as many tick functions as you like, or unregister them using unregister_tick_function(). You can even pass in various parameters that you would like sent to the tick functions. All three of these techniques are shown in this next code block:
<?php
function myfunc($param1, $param2) {
echo "In first tick function with params $param1 $param2\n";
}
function myfunc2($param1, $param2, $param3) {
echo "In second tick function with params $param1 $param2 $param3\n";
}
function myfunc3($param1) {
echo "In third tick function with params $param1\n";
}
register_tick_function("myfunc", "hello", "world");
register_tick_function("myfunc2", "how", "are", "you?");
register_tick_function("myfunc3", "goodbye!");
unregister_tick_function("myfunc2");
declare(ticks=10);
for($i = 0; $i < 20; ++$i) {
echo "Hello\n";
} ?>
Author's Note: Many people use ticks as a method of multithreading or multiprocessing, which is not really ideal. Firstly, the script still executes in parallel: when a tick occurs, PHP stops what it was doing, executes all the tick functions, then goes back to where it left off. Under no situation does PHP execute both the tick function and the original code simultaneously. If you want to have your PHP script do more than one thing at once, flick on ahead to learn about process control.
Also note that, in my opinion, ticks are a dying breed. They were really only implemented as a hack to begin with, and few people even know how they work never mind use them in their own script. Although I doubt ticks will ever be deprecated (the idea itself is logical enough, after all), you should be under no illusions that hiring PHP developers with ticks experiences is very hard!
|
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.
My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!
|