Abstraction: hide the data structure
Change how a class stores its data without touching the code that uses it.
Watch the narrated video
Every step
Problem
Steps 1–3Refactor
Steps 4–8Payoff
Steps 9–10Recap
Steps 11–12The code: before and after
Before
declare(strict_types=1);
final class Warehouse
{
/** @var array<string, int> */
public array $stock = [];
}
final class Receiving
{
public function receive(Warehouse $w, string $sku, int $qty): void
{
$w->stock[$sku] = ($w->stock[$sku] ?? 0) + $qty;
}
}
final class Picking
{
public function pick(Warehouse $w, string $sku, int $qty): void
{
$have = $w->stock[$sku] ?? 0;
if ($have >= $qty) {
$w->stock[$sku] = $have - $qty;
}
}
}
final class Report
{
public function print(Warehouse $w, string $sku): void
{
$left = $w->stock[$sku] ?? 0;
echo "{$sku}: {$left}\n";
}
}
$w = new Warehouse();
(new Receiving())->receive($w, 'TEA-1', 20);
(new Receiving())->receive($w, 'TEA-1', 10);
(new Picking())->pick($w, 'TEA-1', 15);
(new Report())->print($w, 'TEA-1'); // TEA-1: 15After
declare(strict_types=1);
final class Warehouse
{
/** @var array<string, list<int>> */
private array $stock = [];
public function receive(string $sku, int $qty): void
{
$this->stock[$sku][] = $qty;
}
public function pick(string $sku, int $qty): bool
{
if ($this->quantity($sku) < $qty) {
return false;
}
$queue = $this->stock[$sku] ?? [];
while ($qty > 0) {
$used = min($queue[0], $qty);
$queue[0] -= $used;
if ($queue[0] === 0) {
array_shift($queue);
}
$qty -= $used;
}
$this->stock[$sku] = $queue;
return true;
}
public function quantity(string $sku): int
{
return array_sum($this->stock[$sku] ?? []);
}
/** @return list<int> */
public function deliveries(string $sku): array
{
return $this->stock[$sku] ?? [];
}
}
final class Receiving
{
public function receive(Warehouse $w, string $sku, int $qty): void
{
$w->receive($sku, $qty);
}
}
final class Picking
{
public function pick(Warehouse $w, string $sku, int $qty): void
{
$w->pick($sku, $qty);
}
}
final class Report
{
public function print(Warehouse $w, string $sku): void
{
$left = $w->quantity($sku);
echo "{$sku}: {$left}\n";
}
}
$w = new Warehouse();
(new Receiving())->receive($w, 'TEA-1', 20);
(new Receiving())->receive($w, 'TEA-1', 10);
(new Picking())->pick($w, 'TEA-1', 15);
(new Report())->print($w, 'TEA-1'); // TEA-1: 15
echo implode(', ', $w->deliveries('TEA-1')), "\n"; // 5, 10Transcript, step by step
Problem
Step 1: A shop's warehouse stock Before
A small shop's warehouse keeps its stock in a public array, from product code to quantity. Receiving adds a delivery to the array, Picking takes items out when there are enough, and Report prints what's left. Two deliveries of tea come in, 20 and then 10, and after 15 are picked, the report prints 15.
Step 2: Oldest delivery first Code smell
The tea goes stale, so picking must take the oldest delivery first, and ops need the stock kept per delivery. The quick way turns each value in the array into a list of delivery quantities, used as a queue. Receiving now appends to the list, and Picking sums it, then takes from the front in a loop. Report sums the list too, and the run still prints 15.
Step 3: Every caller knows the storage Code smell
Every caller works on the array directly, so each one depends on how the warehouse stores its stock. Receiving uses the array here, Picking here, and the report here. One change to the storage meant rewriting three classes that only wanted to receive, pick and count. The sum over the deliveries now exists twice, once in Picking and once in Report. And when the stock moves to a database next year, all three get rewritten again.
Refactor
Step 4: Refactor first Back it out
Let's take the delivery lists out again. We'll refactor first, so that the warehouse owns its stock, and then we'll change the storage in one place.
Step 5: receive() moves in Move Method
First, the update that adds a delivery moves into Warehouse, as a method called receive. Receiving now calls receive on the warehouse, and it no longer touches the array.
Step 6: pick() moves in Move Method
The check and the update move in next, as pick, which returns true when there was enough stock. Picking calls pick on the warehouse, and the run still prints 15.
Step 7: quantity() moves in Move Method
The lookup moves in last, as quantity, which returns zero for a product the warehouse has never seen. Report asks the warehouse for the quantity, and then prints it.
Step 8: Make the array private Encapsulate Collection
No caller touches the array any more, so the property becomes private, and PHP refuses access from outside the class, which PHPStan catches before the code runs. That's abstraction, where callers use what the warehouse does, receive, pick and quantity, and never see how it stores the stock. Encapsulation hides a single field, and abstraction goes one step further, because it hides how a whole collection is stored.
Payoff
Step 9: Keep stock per delivery Substitute Algorithm
Now the change request comes back, and this time it stays inside Warehouse. The private array now holds a list of deliveries per product, and receive appends to it. Quantity sums the list, and pick checks the quantity, then takes from the oldest delivery first. Each method keeps its signature and gets a new body, and that move is called Substitute Algorithm. Deliveries returns the list, and since PHP copies an array when it's returned, a caller can't change the stock through it. Receiving, Picking and Report didn't change, and the script gains one line that prints the deliveries.
Step 10: One class knows the storage Payoff
The array access is gone from the callers, so Receiving calls receive here, Picking calls pick here, and Report calls quantity here. The sum over the deliveries is written once, in quantity, and pick reuses it for its check. The 15 picked came out of the oldest delivery of 20, so 5 of that one are left, next to the newer 10. The three callers depend only on receive, pick and quantity, so changing how Warehouse stores the stock stayed inside Warehouse.
Recap
Step 11: Where we started, where we ended Before / after
On the left, the stock is a public array, and Receiving, Picking and Report each work on it directly. On the right, the array is private and holds a list of deliveries per product, behind receive, pick and quantity. A caller like Picking makes one call, and the warehouse decides which delivery the items come from.
Step 12: When to reach for it Takeaways
Reach for it when several classes read or change one collection, and the way it's stored may change. Skip it when one short function owns the array and nothing outside reads it, because methods around it add lines and hide nothing. You'll spot the need by keys read or written on another object's array, or by the same lookup copied into several classes.