Reputation: 611
I'd like to know why, how and when to use ticks in PHP:
declare(ticks=1);
// A function called on each tick event
function tick_handler()
{
echo "tick_handler() called\n";
}
register_tick_function('tick_handler');
$a = 1;
if ($a > 0) {
$a += 2;
print($a);
}
Upvotes: 18
Views: 5763
Reputation: 670
I found a particularly interesting use case for ticks not outlined here yet.
I was facing a scenario in my daemon where i wanted to use pcntl_fork and ensure that it happened precisely when i expected too but the symptoms where showing me otherwise. The problem boils down into 2 parts, (1) How zend-ng (PHP7's new engine) collates C executions based on your compiled PHP directives, i.e. between each group of executions we have a 'tick', and (2) How resources are exposed to you in PHP, i.e. file descriptors are created on the O/S by the C code which may or may not be in the expected execution block when compiled from your code into C.
In simple terms, I opened a socket in the parent process and in a child process used it, simple right? well no, the resource in the child process wasn't always there as was expected and in all cases the parent process was not terminated (which normally explains why you lose access to open resources)
Forcing PHP to announce when a tick is done after one execution block actually forced zend-ng to be a little less efficient and ensure my blocks of execution weren running as expected.
Upvotes: 0
Reputation: 5479
Ticks can be used for basic things like:
In PHP 4 you could use ticks to implement exception-like error handling.
Ticks can be used for other things too, like implementing an event driven application (e.g. a game).
Upvotes: 7
Reputation: 90012
One use was outlined by [email protected]:
As Chris already noted, ticks doesn't make your script multi-threaded, but they are still great. I use them mainly for profiling - for example, placing the following at the very beginning of the script allows you to monitor its memory usage:
<?php function profiler($return=false) { static $m=0; if ($return) return "$m bytes"; if (($mem=memory_get_usage())>$m) $m = $mem; } register_tick_function('profiler'); declare(ticks=1); /* Your code here */ echo profiler(true); ?>
This approach is more accurate than calling memory_get_usage only in the end of the script. It has some performance overhead though :)
Another use was described by [email protected]:
as i read about ticks the first time i thought "wtf, useless crap" - but then i discovered some usefull application...
you can declare a tick-function which checks each n executions of your script whether the connection is still alive or not, very usefull for some kind of scripts to decrease serverload
<?php function check_connection() { if (connection_aborted()) { // do something here, e.g. close database connections // (or use a shutdown function for this exit; } } register_tick_function("connection"); declare (ticks=20) { // put your PHP-Script here // you may increase/decrease the number of ticks } ?>
Upvotes: 14
Reputation: 46692
A tick is an event that occurs for every N
low-level statements executed by the parser within the declare
block. The value for N
is specified using ticks=N
within the declare blocks's directive section.
The event(s) that occur on each tick are specified using the register_tick_function()
.
Upvotes: 3