Professional Web Applications Themes

#39048 [NEW]: Static variables and instantiated classes. private static doesn't work. - PHP Bugs

From: matti at nitro dot fi Operating system: * PHP version: 5.1.6 PHP Bug Type: Scripting Engine problem Bug description: Static variables and instantiated classes. private static doesn't work. Description: ------------ self:: doesn't care for inheritance in instantiated classes. self:: doesn't care for private. private & static keywords don't work together. redeclaration of static variables doesn't work even for private static. Reproduce code: --------------- <?php class A { static $a = 1; function show() { echo self::$a; } } class B extends A { static $a = 2; } B::show(); // writes "1" not "2" $b = new B(); $b->show(); ...

  1. #1

    Default #39048 [NEW]: Static variables and instantiated classes. private static doesn't work.

    From: matti at nitro dot fi
    Operating system: *
    PHP version: 5.1.6
    PHP Bug Type: Scripting Engine problem
    Bug description: Static variables and instantiated classes. private static doesn't work.

    Description:
    ------------
    self:: doesn't care for inheritance in instantiated classes.

    self:: doesn't care for private.

    private & static keywords don't work together.

    redeclaration of static variables doesn't work even for private static.

    Reproduce code:
    ---------------
    <?php
    class A {
    static $a = 1;

    function show() {
    echo self::$a;
    }
    }

    class B extends A {
    static $a = 2;
    }


    B::show(); // writes "1" not "2"

    $b = new B();
    $b->show(); // writes "1" not "2"


    print '<hr />';

    class C {
    private static $a = 1;

    function show() {
    echo self::$a;
    }
    }

    class D extends C {
    private static $a = 2;
    }


    D::show(); // writes "1" not "2"

    $d = new D();
    $d->show(); // writes "1" not "2"

    ?>

    Expected result:
    ----------------
    22<hr />22

    Actual result:
    --------------
    11<hr />11

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

  2. #2

    Default #39048 [Opn->Bgs]: Static variables and instantiated classes. private static doesn't work.

    ID: 39048
    Updated by: [email]tony2001php.net[/email]
    Reported By: matti at nitro dot fi
    -Status: Open
    +Status: Bogus
    Bug Type: Scripting Engine problem
    Operating System: *
    PHP Version: 5.1.6
    New Comment:

    "self" (as well as "parent") is resolved in compile time, so it'll
    always point to the class where it was used.
    This is expected behaviour.


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

    [2006-10-05 14:21:35] matti at nitro dot fi

    Description:
    ------------
    self:: doesn't care for inheritance in instantiated classes.

    self:: doesn't care for private.

    private & static keywords don't work together.

    redeclaration of static variables doesn't work even for private static.

    Reproduce code:
    ---------------
    <?php
    class A {
    static $a = 1;

    function show() {
    echo self::$a;
    }
    }

    class B extends A {
    static $a = 2;
    }


    B::show(); // writes "1" not "2"

    $b = new B();
    $b->show(); // writes "1" not "2"


    print '<hr />';

    class C {
    private static $a = 1;

    function show() {
    echo self::$a;
    }
    }

    class D extends C {
    private static $a = 2;
    }


    D::show(); // writes "1" not "2"

    $d = new D();
    $d->show(); // writes "1" not "2"

    ?>

    Expected result:
    ----------------
    22<hr />22

    Actual result:
    --------------
    11<hr />11


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


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

  3. #3

    Default #39048 [Bgs->Opn]: Static variables and instantiated classes. private static doesn't work.

    ID: 39048
    User updated by: matti at nitro dot fi
    Reported By: matti at nitro dot fi
    -Status: Bogus
    +Status: Open
    Bug Type: Scripting Engine problem
    Operating System: *
    PHP Version: 5.1.6
    New Comment:

    Ok. So there must be another way to get static variable than self::$var
    to enable inherited methods that use static variables in subclasses?

    On the otherhand shouldn't private static be only defined in ongoing
    class. In the case of instance class D shouldn't it actually print 2
    from $D->show() since the method is inherited and private static is
    redefined?


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

    [2006-10-05 14:32:48] [email]tony2001php.net[/email]

    "self" (as well as "parent") is resolved in compile time, so it'll
    always point to the class where it was used.
    This is expected behaviour.

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

    [2006-10-05 14:21:35] matti at nitro dot fi

    Description:
    ------------
    self:: doesn't care for inheritance in instantiated classes.

    self:: doesn't care for private.

    private & static keywords don't work together.

    redeclaration of static variables doesn't work even for private static.

    Reproduce code:
    ---------------
    <?php
    class A {
    static $a = 1;

    function show() {
    echo self::$a;
    }
    }

    class B extends A {
    static $a = 2;
    }


    B::show(); // writes "1" not "2"

    $b = new B();
    $b->show(); // writes "1" not "2"


    print '<hr />';

    class C {
    private static $a = 1;

    function show() {
    echo self::$a;
    }
    }

    class D extends C {
    private static $a = 2;
    }


    D::show(); // writes "1" not "2"

    $d = new D();
    $d->show(); // writes "1" not "2"

    ?>

    Expected result:
    ----------------
    22<hr />22

    Actual result:
    --------------
    11<hr />11


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


    --
    Edit this bug report at [url]http://bugs.php.net/?id=39048&edit=1[/url]
    matti at nitro dot fi Guest

  4. #4

    Default #39048 [Opn->Bgs]: Static variables and instantiated classes. private static doesn't work.

    ID: 39048
    Updated by: [email]tony2001php.net[/email]
    Reported By: matti at nitro dot fi
    -Status: Open
    +Status: Bogus
    Bug Type: Scripting Engine problem
    Operating System: *
    PHP Version: 5.1.6
    New Comment:
    >In the case of instance class D shouldn't it actually
    > print 2 from $D->show() since the method is inherited and
    > private static is redefined?
    No, I've already explained why.
    No bug here.


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

    [2006-10-05 14:56:27] matti at nitro dot fi

    Ok. So there must be another way to get static variable than self::$var
    to enable inherited methods that use static variables in subclasses?

    On the otherhand shouldn't private static be only defined in ongoing
    class. In the case of instance class D shouldn't it actually print 2
    from $D->show() since the method is inherited and private static is
    redefined?

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

    [2006-10-05 14:32:48] [email]tony2001php.net[/email]

    "self" (as well as "parent") is resolved in compile time, so it'll
    always point to the class where it was used.
    This is expected behaviour.

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

    [2006-10-05 14:21:35] matti at nitro dot fi

    Description:
    ------------
    self:: doesn't care for inheritance in instantiated classes.

    self:: doesn't care for private.

    private & static keywords don't work together.

    redeclaration of static variables doesn't work even for private static.

    Reproduce code:
    ---------------
    <?php
    class A {
    static $a = 1;

    function show() {
    echo self::$a;
    }
    }

    class B extends A {
    static $a = 2;
    }


    B::show(); // writes "1" not "2"

    $b = new B();
    $b->show(); // writes "1" not "2"


    print '<hr />';

    class C {
    private static $a = 1;

    function show() {
    echo self::$a;
    }
    }

    class D extends C {
    private static $a = 2;
    }


    D::show(); // writes "1" not "2"

    $d = new D();
    $d->show(); // writes "1" not "2"

    ?>

    Expected result:
    ----------------
    22<hr />22

    Actual result:
    --------------
    11<hr />11


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


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

Similar Threads

  1. Replies: 2
    Last Post: December 3rd, 12:47 AM
  2. #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
  3. Replies: 1
    Last Post: October 5th, 11:21 AM
  4. Replies: 1
    Last Post: September 11th, 09:21 PM
  5. Replies: 1
    Last Post: July 18th, 12:45 AM

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