Last active
December 16, 2015 04:49
-
-
Save ScreamingDev/5379826 to your computer and use it in GitHub Desktop.
PHP Trap in Functions with static variables
This file contains 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 A | |
{ | |
function __construct() | |
{ | |
echo get_class($this->getInstance()), PHP_EOL, | |
get_class(static::getInstance()), PHP_EOL, | |
get_class(self::getInstance()), PHP_EOL; | |
} | |
public static function getInstance() | |
{ | |
static $getInstance; | |
if (null == $getInstance) $getInstance = static::makeInstance(); | |
return $getInstance; | |
} | |
public static function makeInstance() | |
{ | |
return new stdClass(); | |
} | |
} | |
$a = new A(); | |
class B extends A | |
{ | |
function __construct() | |
{ | |
echo get_class($this->getInstance()), PHP_EOL, | |
get_class(static::getInstance()), PHP_EOL, | |
get_class(self::getInstance()), PHP_EOL; | |
} | |
public static function makeInstance() | |
{ | |
return new ArrayObject(); | |
} | |
public function __destruct() | |
{ | |
die(); | |
} | |
} | |
$a = new B(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment