How to apply SOLID to this PHP class?

How to apply SOLID to this simple PHP class? This is a simple implementation of a dependency injection container.

<?php namespace Classes;

class Container
{
	/**
	 * An array that contains container's elements
	 * @var array
	 */
	private $container = [];

	/**
	 * Add a new element in the container
	 * @param  string   $name 
	 * @param  callable $callable
	 * @throws \Exception
	 */
	public function bind($name, callable $callable)
	{
		if (is_string($name)) {
			$this->container[$name] = $callable;
		} else {
			throw new \Exception('Containe: You have to provide a name in the bind method');
		}
	}

	/**
	 * Resolve an element from container.
	 * @param  string $name
	 * @return object
	 */
	public function resolve($name)
	{
		if (isset($this->container[$name])) {
			return call_user_func($this->container[$name]);
		} else {
			throw new \Exception('Container: The '. $name . ' was not found in the container');
		}
	}
}
1 Like

That code is so short and simple that it’s hardly even had an opportunity to violate any programming principles. But on the plus side, it looks SOLID to me. :wink:

2 Likes

Thank you for your answer! This is a simple implementation, but how can I go on with the complexity? I mean, I know that I can use the Reflection class provided by PHP to do a lot more, but I want to keep the things simple.So, I have a version, similar like this one, but the other uses to have an $intances array, this array keeps track of all already created instances, so I use the singleton pattern, but in a different way.Is this a good idea?

<?php namespace Classes;

class Container
{
	/**
	 * An array that contains container's elements
	 * @var array
	 */
	private $container = [];

/**
 * An array that contains the instances created.
 * @var array
 */
private $instances = [];

/**
 * Add a new element in the container.
 * @param  string   $name 
 * @param  callable $callable
 * @throws \Exception
 */
public function bind($name, callable $callable)
{
	if (is_string($name)) {
		$this->container[$name] = $callable;
	} else {
		throw new \Exception('Containe: You have to provide a valid name in the bind method');
	}
}

/**
 * Resolve an element from container.
 * @param  string $name
 * @return object
 */
public function resolve($name)
{
	if (isset($this->container[$name])) {
		return call_user_func($this->container[$name]);
	} else {
		throw new \Exception('Container: The '. $name . ' was not found in the container');
	}
}

/**
 * Create instances of elements from container.
 * @param  string $name
 */
public function singleton($name) 
{
	$this->instances[$name] = $this->resolve($name);
}

/**
 * Use the required instance.
 * @param  string $name
 * @return object
 * @throws \Exception
 */
public function resolveSingleton($name)
{
	if (isset($this->instances[$name])) {
		return $this->instances[$name];
	} else {
		throw new \Exception('Container: The '. $name . ' instance  was not found in the container');
	}		
}

}

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.