Object-Oriented Programming (OOP) in PHP - Detailed Guide
Object-Oriented Programming (OOP) in PHP - Detailed Guide
Specialized Guide by Alaa Amer – Professional Web & App Developer
Object-Oriented Programming (OOP) is a powerful programming paradigm that makes code more organized and maintainable. In PHP, OOP has evolved to become the foundation of modern frameworks.
2️⃣ Creating Basic Classes
<?php
class User {
// Properties
private $name;
private $email;
protected $age;
public $isActive;
// Constructor
public function __construct($name, $email, $age) {
$this->name = $name;
$this->email = $email;
$this->age = $age;
$this->isActive = true;
}
// Methods
public function getName() {
return $this->name;
}
public function setName($name) {
if (strlen($name) >= 2) {
$this->name = $name;
return true;
}
return false;
}
public function getInfo() {
return "Name: {$this->name}, Email: {$this->email}";
}
// Destructor
public function __destruct() {
echo "User deleted: {$this->name}";
}
}
// Create object
$user1 = new User("Alaa Amer", "[email protected]", 30);
echo $user1->getInfo();
?>
4️⃣ Inheritance
// Base class
class Vehicle {
protected $brand;
protected $model;
protected $year;
public function __construct($brand, $model, $year) {
$this->brand = $brand;
$this->model = $model;
$this->year = $year;
}
public function getInfo() {
return "{$this->brand} {$this->model} ({$this->year})";
}
public function start() {
return "Vehicle started";
}
}
// Inherited class
class Car extends Vehicle {
private $doors;
private $fuelType;
public function __construct($brand, $model, $year, $doors, $fuelType) {
parent::__construct($brand, $model, $year);
$this->doors = $doors;
$this->fuelType = $fuelType;
}
// Method Overriding
public function start() {
return "Car started with ignition key";
}
public function getCarDetails() {
return $this->getInfo() . " - {$this->doors} doors - {$this->fuelType}";
}
}
// Usage
$car = new Car("Toyota", "Camry", 2023, 4, "Gasoline");
echo $car->getCarDetails();
echo $car->start();
6️⃣ Abstract Classes
abstract class Shape {
protected $color;
public function __construct($color) {
$this->color = $color;
}
// Regular method
public function getColor() {
return $this->color;
}
// Abstract methods - must be implemented in child classes
abstract public function calculateArea();
abstract public function draw();
}
class Circle extends Shape {
private $radius;
public function __construct($color, $radius) {
parent::__construct($color);
$this->radius = $radius;
}
public function calculateArea() {
return pi() * pow($this->radius, 2);
}
public function draw() {
return "Drawing {$this->color} circle with radius {$this->radius}";
}
}
💡 OOP Best Practices
- Follow Single Responsibility Principle: Each class has one purpose only
- Use Inheritance Wisely: Only when there's an "is-a" relationship
- Favor Composition over Inheritance: Use "has-a" instead of "is-a" when possible
- Write Testable Code: Separate responsibilities
- Use Interfaces: To ensure compatibility
Next Step
After mastering OOP, move to Design Patterns and SOLID Principles.
📩 Need help with Object-Oriented Programming?
Article Category
Object-Oriented Programming (OOP) in PHP - Detailed Guide
Comprehensive guide to OOP in PHP with practical examples and real applications to understand fundamental concepts.
Consultation & Communication
Direct communication via WhatsApp or phone to understand your project needs precisely.
Planning & Scheduling
Creating clear work plan with specific timeline for each project phase.
Development & Coding
Building projects with latest technologies ensuring high performance and security.
Testing & Delivery
Comprehensive testing and thorough review before final project delivery.