Launch offer
Business website $150 USD Custom plugin $200 USD Ready in 5 days
Get a quote
OOP

Open/Closed Principle

Make code open for extension but closed for modification.

Open/Closed Principle — a practical guide to OCP SOLID with clear examples you can reuse in real projects.

OOP Tutorial Series (48/65). Prefer one article? Read the complete OOP tutorial.

Short description

Add new behavior via new classes implementing interfaces, instead of editing giant switch statements repeatedly.

OCP sketch

interface Discount { public function apply(float $total): float; }
class PercentageDiscount implements Discount {
    public function __construct(private float $percent) {}
    public function apply(float $total): float { return $total * (1 - $this->percent / 100); }
}

Leave a reply

Your email address will not be published. Required fields are marked *