What are the 4 object oriented principles?
Understanding Object-Oriented Principles in Laravel Development
Object-oriented programming (OOP) is a programming paradigm that allows developers to model real-world behavior using objects. These objects contain both data (properties) and logic (methods). Laravel, being a modern PHP framework, embraces object-oriented design patterns deeply across its codebase.
Understanding the four fundamental OOP principles—Encapsulation, Abstraction, Inheritance, and Polymorphism—can help you write Laravel applications that are clean, scalable, and easy to maintain.
1. Encapsulation
Encapsulation is the practice of restricting direct access to certain components of an object, allowing access only through public methods. This helps protect the integrity of the object's state.
// Bad example class UserProfile { public $email; public $password; }
Exposing sensitive properties like password directly is dangerous and defeats the purpose of encapsulation.
// Good example class UserProfile { private $email; private $password; public function setEmail(string $email) { // Add validation logic here $this->email = $email; } public function getEmail(): string { return $this->email; } }
By using getters and setters, we encapsulate access and can enforce validation or other logic as needed.
2. Abstraction
Abstraction means focusing on what an object does instead of how it does it. This hides unnecessary details and provides a clean interface for interacting with an object.
// Bad example class NotificationService { public function sendEmail($to, $message) { // SMTP configuration and logic... } public function sendSMS($to, $message) { // Twilio or SMS gateway logic... } }
This ties the class to specific implementations and makes it harder to extend.
// Good example interface Notifiable { public function send(string $to, string $message): void; } class EmailNotification implements Notifiable { public function send(string $to, string $message): void { // Send email logic } } class SMSNotification implements Notifiable { public function send(string $to, string $message): void { // Send SMS logic } }
This abstraction allows your application to send notifications without caring about the underlying method.
3. Inheritance
Inheritance allows a class to inherit properties and behaviors from another class. This promotes code reuse and creates a natural hierarchy between classes.
// Bad example class User { public function authenticate() {} public function validateDriverLicense() {} } class Admin extends User { public function managePermissions() {} }
The base User class is doing too much. License validation should not belong to a generic user.
// Good example class User { public function authenticate() {} } class Driver extends User { public function validateDriverLicense() {} } class Admin extends User { public function managePermissions() {} }
With better structure, each class handles only what it’s supposed to.
4. Polymorphism
Polymorphism allows different objects to be treated through a shared interface, even if their behaviors differ. This is especially powerful when used with Laravel’s service container and dependency injection.
// Bad example class PaymentProcessor { public function process($gateway, $amount) { if ($gateway === 'stripe') { // Stripe logic } elseif ($gateway === 'paypal') { // PayPal logic } } }
This tightly couples the processor to gateway types.
interface PaymentGateway { public function charge(float $amount): bool; } class StripeGateway implements PaymentGateway { public function charge(float $amount): bool { // Stripe charge logic return true; } } class PayPalGateway implements PaymentGateway { public function charge(float $amount): bool { // PayPal charge logic return true; } } class PaymentProcessor { public function __construct(private PaymentGateway $gateway) {} public function process(float $amount) { return $this->gateway->charge($amount); } }
Now, the processor can work with any gateway implementation, making it fully polymorphic.
Why These Principles Matter in Laravel
Laravel’s ecosystem—Eloquent models, service containers, contracts, and facades—are built upon these OOP principles. When followed:
- Encapsulation keeps your models clean.
- Abstraction helps you swap services without rewriting code.
- Inheritance helps you structure common behaviors.
- Polymorphism enables flexibility in service bindings and testing.
Whether you’re building custom services, using policies, or creating package APIs, applying these OOP principles improves the quality of your Laravel application.