Last active
September 17, 2020 03:23
-
-
Save vhenzl/3959ee1e7013f34202845b54368b1538 to your computer and use it in GitHub Desktop.
PHP: static variable vs static field
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// https://3v4l.org/ZoCO0 | |
class X | |
{ | |
static $y = 0; | |
static $z = 0; | |
public static function test() | |
{ | |
static $x = 0; | |
echo get_called_class() . ": " . $x . " | " . static::$y . " | " . self::$z . "\n"; | |
$x++; | |
static::$y++; | |
self::$z++; | |
} | |
} | |
class A extends X | |
{ | |
public function do1() | |
{ | |
self::test(); | |
} | |
public static function do2() | |
{ | |
self::test(); | |
} | |
public function do3() | |
{ | |
static::test(); | |
} | |
public static function do4() | |
{ | |
static::test(); | |
} | |
} | |
class B extends X | |
{ | |
public function do1() | |
{ | |
self::test(); | |
} | |
public static function do2() | |
{ | |
self::test(); | |
} | |
public function do3() | |
{ | |
static::test(); | |
} | |
public static function do4() | |
{ | |
static::test(); | |
} | |
} | |
$a1 = new A(); | |
$a2 = new A(); | |
$b1 = new B(); | |
$b2 = new B(); | |
$a1->do1(); | |
$a2->do1(); | |
$a1->do3(); | |
$a2->do3(); | |
$b1->do1(); | |
$b2->do1(); | |
$b1->do3(); | |
$b2->do3(); | |
A::do2(); | |
A::do4(); | |
B::do2(); | |
B::do4(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment