php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #39048 Static variables and instantiated classes. private static doesn't work.
Submitted: 2006-10-05 14:21 UTC Modified: 2006-10-05 15:04 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: matti at nitro dot fi Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.1.6 OS: *
Private report: No CVE-ID: None
 [2006-10-05 14:21 UTC] 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

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-10-05 14:32 UTC] tony2001@php.net
"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:56 UTC] 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 15:04 UTC] tony2001@php.net
>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.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Dec 03 22:00:01 2025 UTC