PHP Basic Tutorial
MySQL Connection
PHP Advanced
PHP OOP
The SOLID principles are a set of five design principles in object-oriented programming that help make software more maintainable, scalable, and robust. They were introduced by Robert C. Martin (Uncle Bob).
Here’s a breakdown of each letter in SOLID:
A class should have only one reason to change.
Each class should do one thing and do it well.
Example:
Instead of having a class that handles both database operations and file logging, you should separate them into two classes.
Software entities (classes, modules, functions) should be open for extension, but closed for modification.
You should be able to extend a class's behavior without modifying its source code.
Example:
Use interfaces or abstract classes so new behavior can be added by creating new subclasses.
Subtypes must be substitutable for their base types.
Objects of a subclass should behave the same as objects of the parent class without altering the program’s correctness.
Example:
If class Bird has a method fly(), then a Penguin subclass shouldn't break this logic since penguins can't fly. So, design the hierarchy accordingly.
No client should be forced to depend on methods it does not use.
Split large interfaces into smaller, specific ones so that implementing classes only need to concern themselves with the methods that are relevant to them.
Example:
Don’t create a Worker interface with work() and eat()—split them into Workable and Eatable.
Depend on abstractions, not on concrete implementations.
High-level modules should not depend on low-level modules. Both should depend on abstractions (e.g., interfaces).
Example:
Instead of directly instantiating a class inside another class, inject the dependency using a constructor or setter.
Late Static Binding in PHP refers to the ability to reference the called class in a context of static inheritance, rather than the class where the method is originally defined.
In regular inheritance, using self:: inside a parent class always refers to that parent class, not to the child class—even if the method was called from the child.
To overcome this, PHP introduced static:: in PHP 5.3, enabling late static binding.
class ParentClass { public static function who() { echo "Parent ".__CLASS__ . "<br/>"; } public static function callWho() { self::who(); // Uses early binding } public static function callWhoLate() { static::who(); // Uses late static binding } } class ChildClass extends ParentClass { public static function who() { echo "Child ".__CLASS__ ; } } ChildClass::callWho(); // Output: Parent ParentClass ChildClass::callWhoLate(); // Output: Child ChildClass
PHP does not support multiple inheritance directly through classes, meaning a class cannot extend more than one class. However, PHP allows a form of multiple inheritance using traits.
PHP avoids multiple class inheritance to prevent ambiguity — for example, when two parent classes have methods with the same name.
Design patterns are proven solutions to common problems in software design. As a senior PHP developer, you should be familiar with several. Here's a quick overview of some commonly used design patterns in PHP, with examples:
Ensures a class has only one instance and provides a global point of access to it.
class Singleton { private static $instance; private function __construct() {} // Prevent direct creation public static function getInstance() { if (self::$instance === null) { self::$instance = new Singleton(); } return self::$instance; } public function sayHello() { echo "Hello from Singleton!"; } } $singleton = Singleton::getInstance(); $singleton->sayHello(); // Outputs: Hello from Singleton!
Creates objects without exposing the instantiation logic.
class Car { public function drive() { echo "Driving a car <br/>"; } } class Bike { public function ride() { echo "Riding a bike <br/>"; } } class VehicleFactory { public static function create($type) { if ($type == 'car') return new Car(); if ($type == 'bike') return new Bike(); throw new Exception("Invalid type"); } } $vehicle = VehicleFactory::create('car'); // creates a Car object $vehicle->drive(); // Output: Driving a car $bike = VehicleFactory::create('bike'); $bike->ride(); // Output: Riding a bike
Selects an algorithm at runtime.
interface PaymentMethod { public function pay($amount); } class CreditCardPayment implements PaymentMethod { public function pay($amount) { echo "Paid $amount via credit card.<br/>"; } } class PayPalPayment implements PaymentMethod { public function pay($amount) { echo "Paid $amount via PayPal.<br/>"; } } class Checkout { private $paymentMethod; public function __construct(PaymentMethod $method) { $this->paymentMethod = $method; } public function pay($amount) { $this->paymentMethod->pay($amount); } } // Pay with PayPal $checkout = new Checkout(new PayPalPayment()); $checkout->pay(100); // Output: Paid 100 via PayPal. $checkout = new Checkout(new CreditCardPayment()); $checkout->pay(200); // Output: Paid 200 via credit card.