-3

How can I set the tick_func for all function

function tick_func(){    //for all func,
  sleep(1);
}

//sample function    
call_any_func();              // this will wait 1 sec.
call_another_func();          // this will wait 1 sec.
misima
  • 421
  • 5
  • 17
  • I want to slow down to 1000 over, function.... function sample_handler(){ if function sleep(1); } – misima Jun 10 '11 at 16:57
  • Are you trying to execute `all_func_sleep()` after every time you call any other function? – Wiseguy Jun 10 '11 at 17:46

2 Answers2

4

Given your new post in addition to this post, I think I get what you want to do: run a function before or after every other function in the entire script.

PHP does not offer this functionality. You must manually type this function call at the start or end of any function you wish to affect.

<?php
function some_func_sleep() {
    sleep(1);
}

function call_any_func() {
    some_func_sleep();
    // whatever you actually want it to do
}

function call_another_func() {
    some_func_sleep();
    // whatever other thing you want it to do
}

call_any_func();
call_another_func();

Ta da. It's not exactly beautiful, but it's clear.

Community
  • 1
  • 1
Matchu
  • 83,922
  • 18
  • 153
  • 160
4

Based on the information you've provided, all I can think to point you to is register_tick_function() as possibly what you're trying to do:

declare(ticks=1);

// using a function as the callback
register_tick_function('all_func_sleep', true);

// your code here...

unregister_tick_function('all_func_sleep');

As Matchu says below, this will call after every statement, instead of on function calls, but if you minimize the amount of code between the register... and unregister_tick_function() calls, you can approximate this functionality.

Problematic
  • 17,567
  • 10
  • 73
  • 85
  • I'm not familiar with ticks, and [the PHP docs](http://us3.php.net/manual/en/control-structures.declare.php#control-structures.declare.ticks) don't offer fantastic examples. What does this *do*, exactly? – Matchu Jun 10 '11 at 17:51
  • 1
    A little more information is offered [here](http://stackoverflow.com/questions/2441479/php-using-declare-what-is-a-tick) (pointing [here](http://www.tuxradar.com/practicalphp/4/21/0)) – Problematic Jun 10 '11 at 17:53
  • @Problematic: interesting feature. I don't think it *quite* meets the OP's requirements, since it seems like it will run after each statement instead of each function call, but, given the unclear question, who knows? +1 – Matchu Jun 10 '11 at 18:07
  • @Matchu: True, but if he has a finite amount of work that he needs to do, and can put it in a loop (or even just procedurally), he can delimit the workspace with `unregister_tick_function()`. I'll update the answer to reflect that. – Problematic Jun 10 '11 at 18:15
  • 1
    Does a tick run on statements inside functions called within the declared zone, or just on the outermost level of the stack? – Matchu Jun 11 '11 at 02:51