register_tick_function
(PHP 4 >= 4.0.3, PHP 5)
register_tick_function — Register a function for execution on each tick
Description
Registers the given function
to be executed when a
tick is called.
Parameters
-
function
-
The function name as a string, or an array consisting of an object and a method.
-
arg
-
-
...
-
Return Values
Returns TRUE
on success or FALSE
on failure.
Examples
Example #1 register_tick_function() example
<?php
declare(ticks=1);
// using a function as the callback
register_tick_function('my_function', true);
// using an object->method
$object = new my_class();
register_tick_function(array(&$object, 'my_method'), true);
?>
Changelog
Version | Description |
---|---|
5.3.0 | Ticks are now supported on threaded web server modules. |
Notes
Warning
register_tick_function() should not be used with threaded web server modules with PHP 5.2 or lower.
Коментарии
I tried the following on an IIS 6/PHP 5 machine:
<?php
set_time_limit(0);
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);
$numbers = array();
for($i=0; $i<1000000; $i++)
{
print($i . "<br />");
}
print(profiler(true));
?>
...and got the following errors:
1) PHP has encountered an Unhandled Exception Code -1073741674 at 0435EC5A
2) PHP has encountered an Access Violation at 02727891
3) PHP has encountered an Access Violation at 02727879
4) PHP has encountered an Access Violation at 00000000
So far the code hasn't crashed the web server, but like the warning says, "register_tick_function() should not be used with threaded web server modules. Ticks are not working in ZTS mode and may crash your web server."
Strangely enough, the code does execute successfully every once in a while.
It looks like register_tick_function() can accept an anonymous function, but unregister_tick_function() cannot
put in in your index to find where your code exited. find die o exit
function shutdown_find_exit()
{
var_dump($GLOBALS['dbg_stack']);
}
register_shutdown_function('shutdown_find_exit');
function write_dbg_stack()
{
$GLOBALS['dbg_stack'] = debug_backtrace();
}
register_tick_function('write_dbg_stack');
declare(ticks=1);
A working example with variable input for validating the asymptotic analysis of your algorithm:
<?php
$n = 1000; // Size of your input
declare(ticks=1);
class Counter {
private $counter = 0;
public function increase()
{
$this->counter++;
}
public function print()
{
return $this->counter;
}
}
$obj = new Counter;
register_tick_function([&$obj, 'increase'], true);
for ($i = 0; $i < 100; $i++)
{
$a = 3;
}
// unregister_tick_function([&$obj, 'increase']);
// Not sure how to de-register, you can use static methods and members in the Counter instead.
var_dump("Number of basic low level operations: " . $obj->print());
?>
Due to an implementation bug, the declare(ticks=1) directive leaked into different compilation units prior to PHP 7.0. This is not how declare() directives, which are per-file or per-scope, are supposed to work.
Therefore there are different implementations between PHP 5.6 and the correct implementation has been added in PHP 7.0. This means the below script will return different results
#index.php
<?php
declare(ticks=1);
$count = 0;
register_tick_function('ticker');
function ticker() {
global $count;
$count++;
}
?>
#inc.php
<?php
$baz = "baz";
$qux = "qux";
?>
Running php index.php in the terminal gives:
PHP 5.6 - 7
PHP 7.0 - 5