In this article we'll talk about the third of the SOLID principles. Wondering "what were those principles again?" Go ahead and read the summary article.
Don't worry, understanding the Liskov Substitution Principle is much easier than it looks. The principle states that you should be able to use any implementation of an abstraction anywhere that abstraction is accepted, but let's simplify that a bit. In plain terms, it says that if a class uses an implementation of an interface, it must be able to use any implementation of that interface without requiring any changes to the class.
Liskov Substitution Principle
This principle states that objects should be replaceable with instances of their subtypes without altering the correctness of the program.
To illustrate this principle, let's keep using our OrderProcessor example from the previous chapter. Take a look at this method:
public function process(Order $order)
{
// Siparişi doğrula...
$this->orders->logOrder($order);
}
Notice that after the Order is validated, we log the order using the OrderRepositoryInterface implementation. Let's say that when our order processing business was young, we stored all of our orders in CSV format on the file system. Our only OrderRepositoryInterface implementation was the CsvOrderRepository. Now, as our order rate grows, we want to use a relational database to store them. So let's look at a possible implementation for our new repository:
class DatabaseOrderRepository implements OrderRepositoryInterface {
protected $connection;
public function connect($username, $password)
{
$this->connection = new DatabaseConnection($username, $password);
}
public function logOrder(Order $order)
{
$this->connection->run('insert into orders values (?, ?)', [
$order->id, $order->amount,
]);
}
}
Now let's examine how we would have to use this implementation:
public function process(Order $order)
{
// Siparişi doğrula...
if ($this->repository instanceof DatabaseOrderRepository)
{
$this->repository->connect('root', 'password');
}
$this->respository->logOrder($order);
}
As you can see, we are forced to check from within our consuming processor class whether the current OrderRepositoryInterface is a database implementation. If it is, we have to connect to the database. This may not seem like a problem in a very small application, but what if the OrderRepositoryInterface is used by dozens of other classes? We would be forced to implement this "bootstrap" code in every consumer. That would be a headache to maintain and prone to bugs, and our application would break if we forgot to update a single consumer.
The example above clearly violates the Liskov Substitution Principle. We cannot inject an implementation of our interface without changing the consumer to call the connect method. Well, now that we've identified the problem, let's fix it. Here is our new DatabaseOrderRepository implementation:
class DatabaseOrderRepository implements OrderRepositoryInterface {
protected $connector;
public function __construct(DatabaseConnector $connector)
{
$this->connector = $connector;
}
public function connect()
{
return $this->connector->bootConnection();
}
public function logOrder(Order $order)
{
$connection = $this->connect();
$connection->run('insert into orders values (?, ?)', [
$order->id, $order->amount,
]);
}
}
Now our DatabaseOrderRepository class manages the database connection, and we can remove our "bootstrap" code from the consuming OrderProcessor:
public function process(Order $order)
{
// Siparişi doğrula...
$this->respository->logOrder($order);
}
With this change, the OrderProcessor consumer class can use either the CsvOrderRepository or DatabaseOrderRepository implementation without any changes. Our code satisfies the Liskov Substitution Principle! Notice that many of the architecture concepts we've discussed are related to knowledge. Specifically, the knowledge a class has of its "surroundings", such as the peripheral code and dependencies that help it do its job. As you work toward a robust application architecture, limiting class knowledge will be a recurring and important theme.
Also note the consequences that violating the Liskov Substitution Principle has for the other principles we've learned. When this principle is broken, the Open Closed Principle must be broken too, because if the consumer has to check for instances of various child classes, it will have to be changed every time a new child class comes along.
Watch for Leaks
You have probably noticed that this principle is closely related to avoiding the "leaky abstractions" we discussed in the previous chapter. Our database repository's leaky abstraction was the first sign that the Liskov Substitution Principle was being violated. Keep an eye out for those leaks!