I did some research on this problem and it is really weird.
Static properties inside methods remain their state between instances of object. Which may be confusing. Also there are two statics one is the static function and the other one is static variable inside method.
It may be connected with autoloader. I did similar example with your but without using static methods but using static variable inside method. The result is 1:1:1 using both autoloader and same file.
<?php
class Base
{
public function t()
{
static $number = 0;
$number++;
var_dump($number);
}
public static function e()
{
static $number = 0;
$number++;
var_dump($number);
}
}
$base = new Base();
$base->t();
$a = new A();
$a->t();
$b = new B();
$b->t();
Also if you won't execute Base::e()
the result is correct.
I've did require_once without autoloading and it still works. So it is definitly because of autoloader.
If you put
require_once "Base.php";
require_once "A.php";
require_once "B.php";
instead of autoloader function it works. Why is that I have no idea I've tried to find anything considering static variables with autoloader but without success. However, this answer may give you some clue.
__autoload
really important to reproduce this problem? Any difference if you import the classes manually? If it works the same without__autoload
, you should take it out of the picture. Otherwise make more explicitly that this is only happening when using__autoload
. – Presiderequire_once
statements instead results in 1,1,1. – Preside