Anonymous class construction
In PHP 7.0 there will be anonymous classes. I don't fully understand your question, but your create_class()
function might look like this:
function create_class(string $key, array &$repository) { $obj = new class($key) { private $key; function __construct($key) { $this->key = $key; } }; $repository[$key] = $obj; return $obj;}
This will instantiate an object with an anonymous class type and register it into the $repository
. To get an object out you use the key you created it with: $repository['it.is.an.example']
.
You can create a dummy class using stdClass
$the_obj = new stdClass();
So basically you want to implement a factory pattern.
Class Factory() { static $cache = array(); public static getClass($class, Array $params = null) { // Need to include the inc or php file in order to create the class if (array_key_exists($class, self::$cache) { throw new Exception("Class already exists"); } self::$cache[$class] = $class; return new $class($params); }}public youClass1() { public __construct(Array $params = null) { ... }}
Add a cache within to check for duplicates