Skip to content

Instantly share code, notes, and snippets.

@tjarrett
Created November 28, 2012 00:57
Show Gist options
  • Save tjarrett/4158294 to your computer and use it in GitHub Desktop.
Save tjarrett/4158294 to your computer and use it in GitHub Desktop.
Inherited getInstance()
<?php
class A
{
protected static $instance = array();
protected function A()
{
}
public static function getInstance()
{
$class = get_called_class();
if ( !isset(self::$instance[$class]) ) {
self::$instance[$class] = new $class();
}
return self::$instance[$class];
}
}
class B extends A
{
protected function B()
{
}
}
echo get_class(A::getInstance());
echo "\n";
echo get_class(B::getInstance());
echo "\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment