Forum und email
declare

declare

Příkaz declare se používá k nastavení prováděcích direktiv pro blok kódu. Syntaxe declare je podobná syntaxi ostatnícj příkazů pro řízení toku:

declare (directive) statement

Část directive umožňuje nastavit chování bloku, který má být ovlivněn pomocí declare. V současnosti je rozpoznávána pouze jediná direktiva: ticks. (Pro více informací viz níže - direktiva ticks)

Část statement bloku declare bude provedena - jak bude provedena a jaké vedlejší efekty nastanou během provádění může záležet na direktivě nastavené v bloku directive.

Ticks

Tick je událost, která nastane pro každých N nízkoúrovňových příkazů provedených parserem uvnitř bloku declare. Hodnota N je specifikována pomocí ticks=N uvnitř sekce directive bloku declare.

Událost(i), která nastane při každém ticku, se specifikuje pomocí register_tick_function(). Více podrobností - viz příklad níže. Uvědomte si, že na každý tick může nastat více než jedna událost.

Příklad 16-1. Profile a section of PHP code

<pre>
<?php
// A function that records the time when it is called
function profile ($dump = FALSE)
{
    static
$profile;

    
// Return the times stored in profile, then erase it
    
if ($dump) {
        
$temp = $profile;
        unset (
$profile);
        return (
$temp);
    }

    
$profile[] = microtime ();
}

// Set up a tick handler
register_tick_function("profile");

// Initialize the function before the declare block
profile ();

// Run a block of code, throw a tick every 2nd statement
declare (ticks=2) {
    for (
$x = 1; $x < 50; ++$x) {
        echo
similar_text (md5($x), md5($x*$x)), "&lt;br&gt;";
    }
}

// Display the data stored in the profiler
print_r (profile (TRUE));
?>
</pre>
The example profiles the PHP code within the 'declare' block, recording the time at which every second low-level statement in the block was executed. This information can then be used to find the slow areas within particular segments of code. This process can be performed using other methods: using ticks is more convenient and easier to implement.

Ticks are well suited for debugging, implementing simple multitasking, backgrounded I/O and many other tasks.

See also register_tick_function() and unregister_tick_function().