PhpDig.net

What is PhpDig?
PhpDig is a PHP MySQL based
Web Spider & Search Engine.




Tick Functions


Ticks provide simulated background processing within PHP.

OverviewTicks were added to PHP 4.0.3 as a way to simulate background processing for blocks of code. They allow one or more functions to be called as a side effect of having expressions evaluated. Simply put, developers can set up functions that are called automatically as the script runs - this is useful for running functions that perform status checking, cleanup, notification, and so on. Ticks can also be indispensable for quick and dirty debugging - see the simple example below.

Note

Used inappropriately, ticks can can help you write code that behaves very strangely! You should have a solid understanding of PHP before you start using ticks in production scripts.



How Ticks WorkTicks work via the conjunction of two different mechanisms - a special type of control block called a declare block and the register_tick_function() function. The declare block allows zero or more expressions to be grouped together, while register_tick_function() allows the developer to set a tick function. The tick function will be called once for every one or more expressions within the declare block.

Declare BlocksDeclare blocks use a structure similar to other control blocks (such as while or for). The block has an initialization block, followed by a body section.

The initialization block (enclosed in parentheses) can contain special declare directives. As of PHP 4.0.6, only one directive is valid: ticks. Syntax for the ticks directive is ticks= n, where n denotes an integer value. The integer value controls how many expressions are evaluated before the tick functions (if any) are called.

The body of the block is enclosed in braces and can contain zero or more expressions. For example:

declare (ticks=1 /* Initialization block */) {
   /* body block */
   "statement 1";
   "statement 2";
   "etc...";
}


Note

As of PHP 4.0.6, declare blocks are used only for implementing ticks - in the future, they may be extended to handle other functionality.



Tick FunctionsA tick function is a normal function or method that has been registered with register_tick_function() . The function can either be a built-in function or a user-defined function. Methods should be registered using array ($object, 'method_name') syntax, as in the following examples:

// Register a built-in function
register_tick_function ('flush');

// Register a user-defined function
function say_hi () {
   echo 'Hi!<br />';
}
register_tick_function ('say_hi');

// Register a method
class foo {
   // Empty constructor to avoid trouble with older versions of PHP
   function foo (){}

   function test () {
      echo "test<br />";
   }
}
$f = new foo ();
register_tick_function (array ($f, 'test'));


At the time a function (or method) is registered, it can be passed one or more arguments. These arguments can be literal values (such as 'Apple' or 42), variables (such as $choices), or variables passed by reference (such as &$HTTP_POST_VARS). For example:

<pre>
<?php
// Print the value of $x every 10 statements
register_tick_function ('printf', "The value of \$x is %d\n", &$x);

declare (ticks=10) {
   for ($x = 1; $x <= 10; ++$x) {
      echo $x, "\n";
   }
}
?>
</pre>

The script will output:
1
2
3
4
5
The value of $x is 5
6
7
8
9
10
The value of $x is 10


Multiple functions may be registed and a function may be registered multiple times. The tick functions are called sequentially in the same order that they were registered.

Tick functions and methods can be unregistered by using unregister_tick_function() .

Configuring TicksNo configuration directives currently affect how ticks function.

Installing Tick SupportTick support is built into PHP by default and cannot be disabled.

Additional InformationFor more information on ticks, see:

  • The PHP Online Manual (http://php.net/manual/)






PHP Functions Essential Reference. Copyright © 2002 by New Riders Publishing (Authors: Zak Greant, Graeme Merrall, Torben Wilson, Brett Michlitsch). This material may be distributed only subject to the terms and conditions set forth in the Open Publication License, v1.0 or later (the latest version is presently available at http://www.opencontent.org/openpub/). The authors of this book have elected not to choose any options under the OPL. This online book was obtained from http://www.fooassociates.com/phpfer/ and is designed to provide information about the PHP programming language, focusing on PHP version 4.0.4 for the most part. The information is provided on an as-is basis, and no warranty or fitness is implied. All persons and entities shall have neither liability nor responsibility to any person or entity with respect to any loss or damage arising from the information contained in this book.

Powered by: vBulletin Version 3.0.7
Copyright ©2000 - 2005, Jelsoft Enterprises Ltd.
Copyright © 2001 - 2005, ThinkDing LLC. All Rights Reserved.