Skip to content

Instantly share code, notes, and snippets.

@ScreamingDev
Last active December 16, 2015 04:49
Show Gist options
  • Save ScreamingDev/5379826 to your computer and use it in GitHub Desktop.
Save ScreamingDev/5379826 to your computer and use it in GitHub Desktop.
PHP Trap in Functions with static variables
<?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