Click here to tell your friends about ticks!
You've probably never used it, never saw it, or never found a practical use for ticks, a underused and powerful feature of PHP that allows you to implement exceptions in PHP4, do intensive debugging and profiling, check database connections, turn PHP into an event driven language, or harness complex control over your code.
Concept of Ticks
From the PHP manual: "A tick is an event that occurs for every N low-level statements executed by the parser within the declare block." So, for example, if N=1, then after every statement, the tick event will be executed. Here's an example:
PHP Code:
// N = 1
$pass = md5('qwerty'); /* Tick executed */
$pass = strrev($pass); /* Tick executed */
echo $pass; /* Tick executed */
PHP Code:
// N = 2
$pass = md5('qwerty');
$pass = strrev($pass); /* Tick executed */
echo $pass;
PHP Code:
// N = 3
$pass = md5('qwerty');
$pass = strrev($pass);
echo $pass; /* Tick executed */
The above code is showing 3 examples. In the first one, N = 1, which means that our tick will be executed after every statement. As we increase N, our tick is executed less and less.
Enabling Ticks
So, how do you use ticks? The above code does not have ticks enabled. To enable them, you use the exclusive declare construct. Declare is a construct for the parser to inject code after the specified "ticks" constant. The syntax for declare is:
PHP Code:
declare (ticks=1)
{
// Code Here
}
So, to make our above example complete:
PHP Code:
declare (ticks=1)
{
$pass = md5('qwerty'); /* Tick executed */
$pass = strrev($pass); /* Tick executed */
echo $pass; /* Tick executed */
}
declare (ticks=2)
{
$pass = md5('qwerty');
$pass = strrev($pass); /* Tick executed */
echo $pass;
}
declare (ticks = 3)
{
$pass = md5('qwerty');
$pass = strrev($pass);
echo $pass; /* Tick executed */
}
We've now enabled ticks for our code. But, it's still useless. As it is right now, nothing is executed in the tick event. Let's define something.
Implementing a Tick
We can define a single function or method to be called using the register_tick_function() function. This specifies the event.
So, all together now:
PHP Code:
declare (ticks=1)
{
// Code Here
}
So, to make our above example complete:
PHP Code:
function profile_memory()
{
echo '<!--' . memory_get_usage() . '-->';
}
register_tick_function('profile_memory');
declare (ticks=1)
{
$pass = md5('qwerty'); /* Tick executed */
$pass = strrev($pass); /* Tick executed */
echo $pass; /* Tick executed */
}
register_tick_function('profile_memory');
declare (ticks=2)
{
$pass = md5('qwerty');
$pass = strrev($pass); /* Tick executed */
echo $pass;
}
register_tick_function('profile_memory');
declare (ticks = 3)
{
$pass = md5('qwerty');
$pass = strrev($pass);
echo $pass; /* Tick executed */
}
The above code shows us how we can monitor memory usage and easily locate the memory intensive parts. We can watch where it creeps up and find ways to cut back on our memory usage. Pretty cool, eh? It sure beats manually putting in profile_memory() every so lines. We can easily disable this by removing our call to register_tick_function.
This totally useless!
Not so. I have found quite a few uses for this:- Writing something that checks to see if the database connection has been dropped. If so, throw an exception or handle it somehow.
- Implement exceptions in PHP4. To do this, you would have a throw() function and a tick function that would check the value of the throw function.
- Monitor memory usage and script executation time. You can easily find the slow parts using ticks.
- Do complex buffering on your code.
- Turn PHP into an event driven programming language. No more exception hacks.
Ticks let you dynamically segment your code. Instead of debugging a PHP file, you can now debug PHP line numbers. You can also use ticks outside of debugging, such as turning PHP into an event driven language. Your tick function could check the value of a variable and call a function once the value has changed.
Put it in the Global Scope
You can have ticks work globally by putting this at the top of your script: PHP Code:
declare(ticks=1);
Do they work in loops?
Yes, they work in loops and will be executed in the iteration of the loops. If ticks=1 and the register_tick_function is profile(), your parser is really writing something like this:
PHP Code:
for ($x = 0; $x < 990; $x++)
{
echo 'Test'; profile();
echo 'Testing'; profile();
}
At first glance, profile() is only being called twice. In reality, it's being called 2*999 times. This is where the true power of ticks come in.
Apache and Windows - Bad Match
Avoid using this on Apache with Windows -- there have been reports of system crashes.