From:             php at ideacode dot com
Operating system: Linux 2.4.18-3
PHP version:      4.3.2
PHP Bug Type:     *Programming Data Structures
Bug description:  static variables in tick handler persist across multiple invocations

Description:
------------
The values of static variables within a tick handler (registered with
register_tick_function) persist across multiple invocations of the
script.

You can run and re-run this script as many times as you like; the static
variable value keeps growing.

This bug is similar to #11536 and #17283, but those were closed due to
lack of response.

Reproduce code:
---------------
      1 function heartbeat($return = false) {
      2     static $static = 0;
      3            $auto   = 0;
      4
      5     if ($return) {
      6         return "static=[$static]\nauto=[$auto]\n";
      7     } else {
      8         $static++;
      9         $auto++;
     10     }
     11
     12 }
     13
     14 register_tick_function('heartbeat');
     15 declare (ticks = 1) {
     16 }
     17 echo heartbeat(true);

Expected result:
----------------
At the end of each run of this script, the value of $static should be 1
and the value of $auto should be 0.

According to the documentation, the tick function (heartbeat) will be
called for every tick (1) statements ecxecuted in the declare block, PLUS
1 for the declare close curly (line 16).  Because there are 0 statements
in the block, I expect 0/1 + 1 callbacks to hearbeat.

Consequently, $static should have a value of 1 (incremented once) and
$auto should have a value of 0 (incremented once but looses value after
each call) when explicitly called on line 18.

If you set ticks = 2, I would expect 0/2 + 1 callbacks. Because there are
no statements in the block, ANY value of ticks should still execute only
once.  Ergo, regardless of the ticks setting, $static should still have a
value of 1 and $auto a value of 0.

Actual result:
--------------
At the end of each run of this script, the value of $static increases
relative to the value it had at the LAST run, and $auto remains at 0
always.  Furthermore, "LAST" appears to refer to the last value in the
particular Apache child. If I set Apache to 1 child only, the value always
increases relative to that single value, rather than bouncing around.

In other words, the static variable within heartbeat appears to retain its
value ACROSS invocations. While this would otherwise be a nice feature,
the value bounces around (ie is not monotonically increasing).

Also, with ticks = 1, the value of the static variable appears to increase
by 2 each time: 2, 4, 6, 8, etc. With ticks = 2, $static increases by 1:
1, 2, 3, etc.  With ticks >= 3, $static doesn't increase at all: 0, 0, 0,
etc.
(This may be a separate bug, but I'd rather just mention it here)

-- 
Edit bug report at http://bugs.php.net/?id=24943&edit=1
-- 
Try a CVS snapshot (php4):  http://bugs.php.net/fix.php?id=24943&r=trysnapshot4
Try a CVS snapshot (php5):  http://bugs.php.net/fix.php?id=24943&r=trysnapshot5
Fixed in CVS:               http://bugs.php.net/fix.php?id=24943&r=fixedcvs
Fixed in release:           http://bugs.php.net/fix.php?id=24943&r=alreadyfixed
Need backtrace:             http://bugs.php.net/fix.php?id=24943&r=needtrace
Try newer version:          http://bugs.php.net/fix.php?id=24943&r=oldversion
Not developer issue:        http://bugs.php.net/fix.php?id=24943&r=support
Expected behavior:          http://bugs.php.net/fix.php?id=24943&r=notwrong
Not enough info:            http://bugs.php.net/fix.php?id=24943&r=notenoughinfo
Submitted twice:            http://bugs.php.net/fix.php?id=24943&r=submittedtwice
register_globals:           http://bugs.php.net/fix.php?id=24943&r=globals
PHP 3 support discontinued: http://bugs.php.net/fix.php?id=24943&r=php3
Daylight Savings:           http://bugs.php.net/fix.php?id=24943&r=dst
IIS Stability:              http://bugs.php.net/fix.php?id=24943&r=isapi
Install GNU Sed:            http://bugs.php.net/fix.php?id=24943&r=gnused

Reply via email to