Created
August 30, 2011 15:57
-
-
Save mrgenixus/1181235 to your computer and use it in GitHub Desktop.
A Generic Container class
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 designed to hold groups of objects | |
* | |
* Designed to prevent multiple identical instances | |
* | |
***************************************************/ | |
interface Containable | |
{ | |
/* this returns the value of what an object considers it's index */ | |
public function getIndex(); | |
/* takes an instantiation signature, and an array of indexes */ | |
public function instanceFromIndex($index); | |
/* should save the state of the object */ | |
public function save(); | |
} | |
class Container { | |
public class defaultType implements Containable{ | |
__construct(){ /* this is a dummy class constructor */ } | |
public function getIndex(){ return 0; } | |
public function instanceFromIndex($index){ return new defaultType(); } | |
} | |
public class standIn{ | |
__construct(&$instance){ $thisInstance = &$instance; } | |
__call($name,$arguments){ return call_user_func_array(array($thisInstance,$name),$arguements); } | |
__get($name) { return $thisInstance->$name; } | |
__set($name,$value) { $thisInstance->$name = $value; } | |
public function instanceof($name){ return $thisInstance instanceof $name; } | |
public function method_exists($name){ return method_exists($thisInstance,$name); } | |
private $thisInstance; | |
} | |
public function __construct($type){ | |
if(class_exists($type) ){ | |
$reflection_instance = new ReflectionClass($type); | |
if ($reflection_instance->implementsInterface('Containable')){ | |
$this->child_type = $type; | |
} else { | |
$this->child_type = 'defaultType'; | |
} | |
} else { | |
$this->child_type = 'defaultType'; | |
} | |
} | |
/* takes the constructors parameters and passes them, by proxy */ | |
public function new(){ | |
array_map('save',$this->instances); | |
try { | |
$myInstance = $reflection_instance->(func_get_args()); | |
if (! array_key_exists($myInstance->getIndex(),$this->instances)){ | |
$instances [$myInstance->getIndex()] = $myInstance; | |
return new standIn($this->instances [$myInstance->getIndex()]); | |
} else { | |
unset($myInstance); | |
} | |
} catch { | |
return false; | |
} | |
} | |
public function getIndex($index){ | |
if (! array_key_exists($index,$this->instances){ | |
$myInstace = $this->child_type::instanceFromIndex($index); | |
$this->instances[$index] = $myInstance; | |
} | |
return new standIn($instances[$index]); | |
} | |
private var $child_type; | |
private var $instances; | |
private var $reflection_instance; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The notion is to basically create an application cache of the table, and to prevent creating multiple identical instances of a class. If your application instances multiple users, for example: you don't want to work directly with the database, you want to encapsulate the data in an object, but sometimes, your users may have an application-oriented state that needs to be maintained across all the instances for a given ID.
Some substantial weaknesses:
instanceof and method_exists would break given the current Implementation.