What is it, and what is a tick event in PHP?

5

I was looking at some functions in PHP.net, and I came across a function called declare , and in the example, there is something like this:

declare(ticks=1);

Below is another example:

declare(ticks=1);

// A function called on each tick event
function tick_handler() {
    echo "tick_handler() called\n";
}
register_tick_function('tick_handler');

As I understand it, it is a method that is called every time the value of a variable is accessed / changed. Even reading the documentation, I did not understand exactly what this method does. In what situations could it be useful? What is a tick event ?

    
asked by anonymous 02.02.2014 / 00:23

2 answers

4
  

In what situations could it be useful?

Adding to the mlemos response, giving more concrete examples ...

Ticks have some utility in debugging. For example, to define a memory usage profile (code optimization or memory leak detection). In an intensive script that sometimes exceeds memory limits, I can use this feature to set X to X to detect how much memory is being used by the process. I also used tests on PHP builds without active debugging, in the final stage of developing C extensions for PHP, to simulate break-points in code.

As this feature allows you to simulate "multi-threading", it can be used to simulate parallel computing. Some uses mentioned may be:

  • create resource checking routines, such as whether a connection remains active.
  • Simulate a simple Event Driven Application.
  • However, since the interpreter stops executing the "normal" code to execute the tick handler, if the code is too long, it can make the application very slow.

        
    02.02.2014 / 10:59
    4

    Functions registered as tick handlers are invoked after Zend Engine executes one or more PHP commands, not just when variables are changed.

    This serves to execute tasks in parallel emulating a limited species of multi-tasking.

    It is not a very used thing because it can make your scripts very slow, so there are not many useful applications for this function.

        
    02.02.2014 / 01:21