protected function getClosure(string $class_name, string $class_key): callable
    {
        $parent_class_names = class_parents($class_name);
        $all_class_names    = $parent_class_names;
        $all_class_names[]  = $class_name;

        $class                      = new \ReflectionClass($class_name);
        $constructor                = $class->getConstructor(); // Null if no constructor.
        $class_rule                 = $this->getRule($class_name, $class_key, $parent_class_names);
        $constructor_params_closure = $constructor ? $this->getParamsClosure($constructor, $class_rule) : null;

        if (!$this->rules['*']['new_instances'] || !array_intersect($all_class_names, $this->rules['*']['new_instances'])) {
            return function (array $args) use ($class, $class_key, $constructor, $constructor_params_closure) {
                if ($constructor && $constructor_params_closure) {
                    return $this->instances[$class_key] = $class->newInstanceArgs($constructor_params_closure($args));
                } else {
                    return $this->instances[$class_key] = $class->newInstanceWithoutConstructor();
                }
            };
        } elseif ($constructor && $constructor_params_closure) {
            return function (array $args) use ($class, $constructor_params_closure) {
                return $class->newInstanceArgs($constructor_params_closure($args));
            };
        } else {
            return function (array $args) use ($class) {
                return $class->newInstanceWithoutConstructor();
            };
        }
    }