What is a "tick Event" in PHP?

Asked

Viewed 299 times

6

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

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');

From what I understand, 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?

  • Related: http://answall.com/questions/52192/para-que-serve-a-palavra-chave-declare-no-php

2 answers

5


In what situations it could be useful?

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

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

As this functionality allows simulating "multi-threading", it can be used to simulate parallel computing. Some uses referred to can be:

  1. create resource check routines such as if a connection is still active.
  2. Simulate a simple event-driven application (Event Driven Application).

However, as the interpreter stops running the "normal" code to run the Handler tick, if the code is too long, it can make the application too slow.

4

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

This serves to perform tasks in parallel by emulating a limited kind of multi-task.

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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.