| Pattern | Usage | Common In | Use in Core PHP |
|---|---|---|---|
| ⭐ MVC | Very High | Laravel, CodeIgniter, Symfony | Build web applications with separate Model, View, and Controller |
| ⭐ Dependency Injection (DI) | Very High | Laravel Service Container | Inject database, mail, logger, or service classes into other classes |
| ⭐ Repository | Very High | Large applications | Separate database queries from business logic |
| ⭐ Factory | Very High | Model factories, object creation | Create objects like Payment, Notification, or Database drivers |
| ⭐ Singleton | High | Service container, configuration | Single database connection, configuration manager, logger |
| ⭐ Facade | Very High | DB, Cache, Auth, Mail | Provide one simple interface to multiple classes (e.g., DatabaseFacade) |
| ⭐ Strategy | High | Payment gateways, notifications | Switch between payment methods, tax calculation, or file upload strategies |
| ⭐ Observer | High | Model events | Send email, SMS, or log events when data changes |
| ⭐ Builder | High | Query Builder, Eloquent | Build SQL queries or complex objects step by step |
| ⭐ Adapter | Medium | Third-party APIs | Integrate different payment gateways or external APIs with a common interface |
| ⭐ Command | Medium | Artisan commands, queues | Execute background jobs, scheduled tasks, or file processing |
1. MVC (Model-View-Controller):
Separate your code into three parts.
- Model → Data (Database)
- View → UI (HTML)
- Controller → Connects Model and View
// Model
class User
{
public function getName()
{
return "John";
}
}
// Controller
$user = new User();
$name = $user->getName();
// View
echo $name;2. Factory Pattern:
Creates one object based on input
Suppose you want different payment methods.
class PayPal
{
public function pay()
{
echo "Paid using PayPal";
}
}
class Stripe
{
public function pay()
{
echo "Paid using Stripe";
}
}
class PaymentFactory
{
public function create($type)
{
if ($type == "paypal") {
return new PayPal();
}
return new Stripe();
}
}$factory = new PaymentFactory();
$payment = $factory->create("paypal");
$payment->pay();3. Facade Pattern:
Uses multiple objects to perform one task
Suppose processing an order requires:
- Payment
- Inventory
class Payment
{
public function pay()
{
echo "Payment Done<br>";
}
}
class Inventory
{
public function update()
{
echo "Stock Updated<br>";
}
}
class Email
{
public function send()
{
echo "Email Sent<br>";
}
}class OrderFacade
{
public function placeOrder()
{
$payment = new Payment();
$inventory = new Inventory();
$email = new Email();
$payment->pay();
$inventory->update();
$email->send();
}
}$order = new OrderFacade();
$order->placeOrder();4. Singleton Design Pattern:
- Private Static variable
- Private construct
- Public static method return class single instance.
class Singleton
{
private static $instance = null;
private function __construct()
{
echo "connect";
}
public static function showInstance()
{
if(self::$instance == null)
{
self::$instance = new static();
}
else
{
echo "Already connected";
}
}
}
$obj1= Singleton::showInstance();5. Observer Design Pattern:
One object changes → Many objects get notified automatically.
When a new video is uploaded, all subscribers receive a notification.
- YouTube Channel = Subject
- Subscribers = Observers
Observer:
interface Observer
{
public function update($message);
}
class User implements Observer
{
public function update($message)
{
echo "Notification: $message <br>";
}
}Subject:
class Channel
{
private $observers = [];
public function subscribe(Observer $observer)
{
$this->observers[] = $observer;
}
public function notify($message)
{
foreach ($this->observers as $observer) {
$observer->update($message);
}
}
}Use It:
$channel = new Channel();
$user1 = new User();
$user2 = new User();
$channel->subscribe($user1);
$channel->subscribe($user2);
$channel->notify("New Video Uploaded");