Alaa Amer Articles

We offer a comprehensive collection of essential educational articles in web development to turn your ideas into digital reality

Object-Oriented Programming (OOP) in PHP - Detailed Guide

PHP 2025-12-31 Alaa Amer

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

  1. Follow Single Responsibility Principle: Each class has one purpose only
  2. Use Inheritance Wisely: Only when there's an "is-a" relationship
  3. Favor Composition over Inheritance: Use "has-a" instead of "is-a" when possible
  4. Write Testable Code: Separate responsibilities
  5. Use Interfaces: To ensure compatibility

Next Step

After mastering OOP, move to Design Patterns and SOLID Principles.

📩 Need help with Object-Oriented Programming?

PHP OOP Classes Objects Inheritance Polymorphism
Article Category
PHP

Object-Oriented Programming (OOP) in PHP - Detailed Guide

Comprehensive guide to OOP in PHP with practical examples and real applications to understand fundamental concepts.

Object-Oriented Programming (OOP) in PHP - Detailed Guide
01

Consultation & Communication

Direct communication via WhatsApp or phone to understand your project needs precisely.

02

Planning & Scheduling

Creating clear work plan with specific timeline for each project phase.

03

Development & Coding

Building projects with latest technologies ensuring high performance and security.

04

Testing & Delivery

Comprehensive testing and thorough review before final project delivery.

Alaa Amer
Alaa Amer

Professional web developer with over 10 years of experience in building innovative digital solutions.

Need This Service?

Contact me now for a free consultation and quote

WhatsApp Your satisfaction is our ultimate goal

What We Offer

  • Website Maintenance & Updates

    Keep your website secure updated optimized

  • API Integration

    Connect your systems with powerful APIs

  • Database Design & Optimization

    Faster queries cleaner structure fewer issues

  • Website Security Hardening

    Protect your site from cyber threats

  • Automation & Scripts

    Automate repetitive tasks and save time

Have Questions?

Call Us Now

00201014714795