<?php

class Factory
{
    protected $items;

    public function __construct(array $items = [])
    {
        $this->items = $items;
    }

    public function register($key, $value = null)
    {
        if (is_array($key)) {
            foreach ($key as $alias => $class) {
                $this->register($alias, $class);
            }
        } else {
            $this->items[$key] = $value;
        }

    }

    public function create($key)
    {
        if (array_key_exists($key, $this->items)) {
            return new $this->items[$key];
        }

        throw new InvalidArgumentException("Key [{$key}] is not registered.");
    }
}

// Test

$factory = new Factory;

$factory->register('std', StdClass::class);
$factory->register('dom', DOMDocument::class);
$factory->register('exception', Exception::class);

$classes = [
    'time' => DateTime::class,
    'queue' => SplQueue::class,
    'array' => ArrayObject::class,
];

$factory->register($classes);

var_dump(
    $factory->create('dom'),
    $factory->create('queue'),
    $factory->create('exception')
);