Professional Web Applications Themes

#39713 [NEW]: Static variables defined in static methods duplicated in derived classes - PHP Bugs

From: paul at digitalbacon dot us Operating system: Linux, Mac OS X 10.4, Windows XP PHP version: 5.2.0 PHP Bug Type: Scripting Engine problem Bug description: Static variables defined in static methods duplicated in derived classes Description: ------------ When using a static variable defined inside a parent class's static method block, different outcomes can occur depending on the scope name used when calling. The effect is that a non-overridden method in the child class is allocated a completely separate static variable... not how it works in C or C++, which I know PHP is not. I encountered this problem implementing ...

  1. #1

    Default #39713 [NEW]: Static variables defined in static methods duplicated in derived classes

    From: paul at digitalbacon dot us
    Operating system: Linux, Mac OS X 10.4, Windows XP
    PHP version: 5.2.0
    PHP Bug Type: Scripting Engine problem
    Bug description: Static variables defined in static methods duplicated in derived classes

    Description:
    ------------
    When using a static variable defined inside a parent class's
    static method block, different outcomes can occur depending on
    the scope name used when calling.

    The effect is that a non-overridden method in the child class
    is allocated a completely separate static variable... not how
    it works in C or C++, which I know PHP is not.

    I encountered this problem implementing a singleton pattern in
    a parent class. I apologize for the reproduction code, but it
    does illustrate my perceived problem.

    The closest bugs I found, relate to static class attributes,
    not regular static variables.

    Reproduce code:
    ---------------
    <?php

    class TheParent {
    public static function staticMethod()
    {
    static $theStaticValue = true;

    if ($theStaticValue == true) {
    $theStaticValue = false;
    return true;
    } else return $theStaticValue; // false
    }
    }

    class TheChild extends TheParent {}

    $a1 = TheParent::staticMethod();
    $a2 = TheParent::staticMethod();
    $b1 = TheChild::staticMethod();
    $b2 = TheChild::staticMethod();

    echo '$a1: ', $a1, "\n"; // true
    echo '$a2: ', $a2, "\n"; // false
    echo '$b1: ', $b1, "\n"; // true (I believe this should be false)
    echo '$b2: ', $b2, "\n"; // false
    ?>

    Expected result:
    ----------------
    My intuition tells me that the parent's static variable should
    be used regardless of the scope name used... unless it's
    actually redefined in the child class.

    In this example, I expect the output to be:

    $a1: 1
    $a2:
    $b1:
    $b2:

    Is local static method data not shared?

    Actual result:
    --------------
    $a1: 1
    $a2:
    $b1: 1
    $b2:

    --
    Edit bug report at [url]http://bugs.php.net/?id=39713&edit=1[/url]
    --
    Try a CVS snapshot (PHP 4.4): [url]http://bugs.php.net/fix.php?id=39713&r=trysnapshot44[/url]
    Try a CVS snapshot (PHP 5.2): [url]http://bugs.php.net/fix.php?id=39713&r=trysnapshot52[/url]
    Try a CVS snapshot (PHP 6.0): [url]http://bugs.php.net/fix.php?id=39713&r=trysnapshot60[/url]
    Fixed in CVS: [url]http://bugs.php.net/fix.php?id=39713&r=fixedcvs[/url]
    Fixed in release: [url]http://bugs.php.net/fix.php?id=39713&r=alreadyfixed[/url]
    Need backtrace: [url]http://bugs.php.net/fix.php?id=39713&r=needtrace[/url]
    Need Reproduce Script: [url]http://bugs.php.net/fix.php?id=39713&r=needscript[/url]
    Try newer version: [url]http://bugs.php.net/fix.php?id=39713&r=oldversion[/url]
    Not developer issue: [url]http://bugs.php.net/fix.php?id=39713&r=support[/url]
    Expected behavior: [url]http://bugs.php.net/fix.php?id=39713&r=notwrong[/url]
    Not enough info: [url]http://bugs.php.net/fix.php?id=39713&r=notenoughinfo[/url]
    Submitted twice: [url]http://bugs.php.net/fix.php?id=39713&r=submittedtwice[/url]
    register_globals: [url]http://bugs.php.net/fix.php?id=39713&r=globals[/url]
    PHP 3 support discontinued: [url]http://bugs.php.net/fix.php?id=39713&r=php3[/url]
    Daylight Savings: [url]http://bugs.php.net/fix.php?id=39713&r=dst[/url]
    IIS Stability: [url]http://bugs.php.net/fix.php?id=39713&r=isapi[/url]
    Install GNU Sed: [url]http://bugs.php.net/fix.php?id=39713&r=gnused[/url]
    Floating point limitations: [url]http://bugs.php.net/fix.php?id=39713&r=float[/url]
    No Zend Extensions: [url]http://bugs.php.net/fix.php?id=39713&r=nozend[/url]
    MySQL Configuration Error: [url]http://bugs.php.net/fix.php?id=39713&r=mysqlcfg[/url]
    paul at digitalbacon dot us Guest

  2. #2

    Default #39713 [Opn->Bgs]: Static variables defined in static methods duplicated in derived classes

    ID: 39713
    Updated by: [email]tony2001php.net[/email]
    Reported By: paul at digitalbacon dot us
    -Status: Open
    +Status: Bogus
    Bug Type: Scripting Engine problem
    Operating System: Linux, Mac OS X 10.4, Windows XP
    PHP Version: 5.2.0
    New Comment:

    Thank you for taking the time to write to us, but this is not
    a bug. Please double-check the doentation available at
    [url]http://www.php.net/manual/[/url] and the instructions on how to report
    a bug at [url]http://bugs.php.net/how-to-report.php[/url]




    Previous Comments:
    ------------------------------------------------------------------------

    [2006-12-02 04:24:04] paul at digitalbacon dot us

    Description:
    ------------
    When using a static variable defined inside a parent class's
    static method block, different outcomes can occur depending on
    the scope name used when calling.

    The effect is that a non-overridden method in the child class
    is allocated a completely separate static variable... not how
    it works in C or C++, which I know PHP is not.

    I encountered this problem implementing a singleton pattern in
    a parent class. I apologize for the reproduction code, but it
    does illustrate my perceived problem.

    The closest bugs I found, relate to static class attributes,
    not regular static variables.

    Reproduce code:
    ---------------
    <?php

    class TheParent {
    public static function staticMethod()
    {
    static $theStaticValue = true;

    if ($theStaticValue == true) {
    $theStaticValue = false;
    return true;
    } else return $theStaticValue; // false
    }
    }

    class TheChild extends TheParent {}

    $a1 = TheParent::staticMethod();
    $a2 = TheParent::staticMethod();
    $b1 = TheChild::staticMethod();
    $b2 = TheChild::staticMethod();

    echo '$a1: ', $a1, "\n"; // true
    echo '$a2: ', $a2, "\n"; // false
    echo '$b1: ', $b1, "\n"; // true (I believe this should be false)
    echo '$b2: ', $b2, "\n"; // false
    ?>

    Expected result:
    ----------------
    My intuition tells me that the parent's static variable should
    be used regardless of the scope name used... unless it's
    actually redefined in the child class.

    In this example, I expect the output to be:

    $a1: 1
    $a2:
    $b1:
    $b2:

    Is local static method data not shared?

    Actual result:
    --------------
    $a1: 1
    $a2:
    $b1: 1
    $b2:


    ------------------------------------------------------------------------


    --
    Edit this bug report at [url]http://bugs.php.net/?id=39713&edit=1[/url]
    tony2001@php.net Guest

  3. #3

    Default #39713 [Bgs]: Static variables defined in static methods duplicated in derived classes

    ID: 39713
    User updated by: paul at digitalbacon dot us
    Reported By: paul at digitalbacon dot us
    Status: Bogus
    Bug Type: Scripting Engine problem
    Operating System: Linux, Mac OS X 10.4, Windows XP
    PHP Version: 5.2.0
    New Comment:

    From the manual:

    ....A static variable exists only in a local function scope,
    but it does not lose its value when program execution leaves
    this scope.

    I don't see why the use of the scope resolution operator
    (::) should change a static method's local function scope.
    Can a single static method really have multiple local scopes
    if using inheritance?


    Previous Comments:
    ------------------------------------------------------------------------

    [2006-12-02 11:49:09] [email]tony2001php.net[/email]

    Thank you for taking the time to write to us, but this is not
    a bug. Please double-check the doentation available at
    [url]http://www.php.net/manual/[/url] and the instructions on how to report
    a bug at [url]http://bugs.php.net/how-to-report.php[/url]



    ------------------------------------------------------------------------

    [2006-12-02 04:24:04] paul at digitalbacon dot us

    Description:
    ------------
    When using a static variable defined inside a parent class's
    static method block, different outcomes can occur depending on
    the scope name used when calling.

    The effect is that a non-overridden method in the child class
    is allocated a completely separate static variable... not how
    it works in C or C++, which I know PHP is not.

    I encountered this problem implementing a singleton pattern in
    a parent class. I apologize for the reproduction code, but it
    does illustrate my perceived problem.

    The closest bugs I found, relate to static class attributes,
    not regular static variables.

    Reproduce code:
    ---------------
    <?php

    class TheParent {
    public static function staticMethod()
    {
    static $theStaticValue = true;

    if ($theStaticValue == true) {
    $theStaticValue = false;
    return true;
    } else return $theStaticValue; // false
    }
    }

    class TheChild extends TheParent {}

    $a1 = TheParent::staticMethod();
    $a2 = TheParent::staticMethod();
    $b1 = TheChild::staticMethod();
    $b2 = TheChild::staticMethod();

    echo '$a1: ', $a1, "\n"; // true
    echo '$a2: ', $a2, "\n"; // false
    echo '$b1: ', $b1, "\n"; // true (I believe this should be false)
    echo '$b2: ', $b2, "\n"; // false
    ?>

    Expected result:
    ----------------
    My intuition tells me that the parent's static variable should
    be used regardless of the scope name used... unless it's
    actually redefined in the child class.

    In this example, I expect the output to be:

    $a1: 1
    $a2:
    $b1:
    $b2:

    Is local static method data not shared?

    Actual result:
    --------------
    $a1: 1
    $a2:
    $b1: 1
    $b2:


    ------------------------------------------------------------------------


    --
    Edit this bug report at [url]http://bugs.php.net/?id=39713&edit=1[/url]
    paul at digitalbacon dot us Guest

Similar Threads

  1. #39664 [NEW]: Invokation of non-static method as static issue
    By john dot hargrove at sellingsource dot com in forum PHP Bugs
    Replies: 3
    Last Post: November 28th, 10:33 PM
  2. Replies: 3
    Last Post: October 5th, 03:04 PM
  3. Replies: 1
    Last Post: October 5th, 11:21 AM
  4. Replies: 1
    Last Post: September 11th, 09:21 PM
  5. Why not use static/shared methods?
    By Karl Seguin in forum ASP.NET General
    Replies: 1
    Last Post: July 7th, 02:01 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139