Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do php's declare(ticks) really work?

Tags:

php

declare

i created a signal handling class using pcntl_signal which now i want to use for sigalrm

the problem i have is that my phpunit test for testing the signalclass works (where im only using declare ticks in the signalclass), but the testclass for testing the alarm class, which in turn using the signalclass doesnt if i add declare(ticks=1) in my alarmtests it also works

i thought declare ticks is only needed at the signal handling code, which in my case is in the signalclass? but as far as i can see it is also needed for the code who calls signal handling code it doesnt even work in my alarmclass, i have to put it in my alarmtest class!?

altough using strace the signal is delivered independent of ticks

so anyone understands why i have to use declare() in my tests (sometimes)? or why do i need to declare(ticks=1) also in the code which uses it? it would mean a user needs to know how to use declare

like image 983
John Doe Avatar asked Dec 22 '09 06:12

John Doe


1 Answers

To use ticks in the global scope you have to have it in the beginning of the calling script. This is probably why you have to redeclare it. I cannot say for certain without knowing your code. Below are some examples that do work with unit testing.

You can IIRC declare youre ticks in your code by the following construct

function tick_function() {
    // Do something
}

register_tick_function('tick_function');

declare (ticks=1) {
    // Your code here
}

Or as a working example

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 */
}

This is a working example of a self contained tick function that is working within a unit test

class TickTest {
    private function profile_memory() {
        static $i;
        ++$i;
        echo "Tick $i\n";
    }
    public function  __construct() {
    }
    public function doTicks() {
        $register_tick_function = register_tick_function(
                array($this,'profile_memory')
            );
        declare (ticks=1) {
            $pass = md5('qwerty'); /* Tick executed */
            $pass = strrev($pass);  /* Tick executed */
        }
    }
}

And this is the unit test (and yes i'm aware of the fact that it's not a real test)

require_once 'PHPUnit/Framework.php';
require_once dirname(__FILE__).'/../ticks.php';
class TickTestTest extends PHPUnit_Framework_TestCase {
    protected $object;
    protected function setUp() {
        $this->object = new TickTest;
    }
    protected function tearDown() {
    }
    public function testDoTicks() {
        $this->object->doTicks();
    }
}

Looking at the output the tick function is called when executing the unit test.

Some references

  • Tick functions
  • Ticks
like image 141
Peter Lindqvist Avatar answered Oct 27 '22 20:10

Peter Lindqvist