Doc #66627 [NEW]: Static variables in inherited classes not explained
| From: | kulakov74 at yandex dot ru | Date: | Sun, 02 Feb 2014 23:52:23 +0000 |
| Subject: | Doc #66627 [NEW]: Static variables in inherited classes not explained | ||
| Groups: | php.doc.bugs | ||
| Request: | Send a blank email to doc-bugs+get-10938@lists.php.net to get a copy of this message | ||
From: kulakov74 at yandex dot ru
Operating system: Any
PHP version: 5.4.24
Package: Documentation problem
Bug Type: Documentation Problem
Bug description:Static variables in inherited classes not explained
Description:
------------
Studying Traits (php.net/traits), I found the example (#9) with static
variables which produced different results from what I expected, that is
the static variable $c was not shared in the 2 classes using the trait
Counter. I tried some tests with traits and classes and came to the
conclusion that the behaviour is correct but not documented well in the
case of class inheritance. Static variables are really tied to the
function they are declared in but if the function is a class method and
the class is inherited from, the method may be internally duplicated (I
believe it's implemented this way) to the child class where it actually
becomes a different (albeit identical) method hence it holds its own
copy of the same static variable. By duplicating I mean the resulting
behavior is just like the methods are really declared in the child.
In my tests I found out the following:
-A static variable in a class method acts just like one in a global
function (that's almost obvious).
-If the class is extended then all its methods except the constructor (&
destructor) and the private methods are internally duplicated to the
child. Public and protected methods can be called in the child hence
they need to be duplicated in it. Constructors and privates are specific
to the base class hence their static vars are shared between all
children.
Traits exhibit similar behavior. This is my understanding of the
behavior, it may be wrong but anyway the behavior should explained in
more detail in the manual.
Related bugs:
https://bugs.php.net/bug.php?id=39713
https://bugs.php.net/bug.php?id=62547
https://bugs.php.net/bug.php?id=63454
Test script:
---------------
class B{
function __construct(){static $con=0; $con++;
echo(get_class($this)."::con:$con; ");}
function pub(){static $pub=0; $pub++;
echo(get_class($this)."::pub:$pub; ");}
function test(){$this->prv(); $this->prot();}
private function prv(){static $prv=0; $prv++;
echo(get_class($this)."::prv:$prv; ");}
protected function prot(){static $prot=0; $prot++;
echo(get_class($this)."::prot:$prot; ");}
}
class C1 extends B{} class C2 extends B{}
$B1=new B; $B2=new B; $C1=new C1; $C2=new C2; echo("\n");
$B1->pub(); $B2->pub(); $B1->pub(); $B2->pub(); echo("\n");
$C1->pub(); $C1->pub(); $C2->pub(); $C2->pub(); echo("\n");
$B1->test(); $B2->test(); echo("\n");
$C1->test(); $C1->test(); $C2->test(); $C2->test(); echo("\n");
Expected result:
----------------
---
Actual result:
--------------
B::con:1; B::con:2; C1::con:3; C2::con:4;
B::pub:1; B::pub:2; B::pub:3; B::pub:4;
C1::pub:1; C1::pub:2; C2::pub:1; C2::pub:2;
B::prv:1; B::prot:1; B::prv:2; B::prot:2;
C1::prv:3; C1::prot:1; C1::prv:4; C1::prot:2; C2::prv:5; C2::prot:1;
C2::prv:6; C2::prot:2;
--
Edit bug report at https://bugs.php.net/bug.php?id=66627&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=66627&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=66627&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=66627&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=66627&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=66627&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=66627&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=66627&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=66627&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=66627&r=support
Expected behavior: https://bugs.php.net/fix.php?id=66627&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=66627&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=66627&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=66627&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=66627&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=66627&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=66627&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=66627&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=66627&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=66627&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=66627&r=mysqlcfg