Created
August 15, 2011 10:14
-
-
Save sunaot/1146000 to your computer and use it in GitHub Desktop.
how static variable works in PHP.
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 | |
class Base | |
{ | |
static public $name = 'BASE'; | |
function stamp($name) { | |
static::$name = $name; | |
} | |
} | |
class A extends Base | |
{ | |
static public $name = 'A'; | |
} | |
class B extends Base | |
{ | |
static public $name = 'B'; | |
} | |
$a = new A; | |
$b = new B; | |
assert('A' === $a::$name); | |
assert('B' === $b::$name); | |
$a->stamp('Z'); | |
assert('Z' === $a::$name); | |
assert('B' === $b::$name); | |
class C extends Base {} | |
class D extends Base {} | |
$c = new C; | |
$d = new D; | |
assert('BASE' === $c::$name); | |
assert('BASE' === $d::$name); | |
$c->stamp('Z'); | |
assert('Z' === $c::$name); | |
assert('Z' === $d::$name); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment