Skip to content

dr-fet91/oop-training-php

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

💡 SOLID Principles in PHP (Object-Oriented Practice)

این پروژه تمرینی با هدف درک و پیاده‌سازی اصول OOP و SOLID در زبان PHP طراحی شده است. تمام اصول به صورت گام‌به‌گام با مثال‌های عملی پیاده‌سازی شده‌اند.


🛠️ Tech Stack

  • PHP 8.1+
  • Composer
  • PSR-4 Autoloading

📁 ساختار پروژه

oop/
├── index.php
├── composer.json
├── src/
│   ├── Interfaces/
│   │   ├── CanLogin.php
│   │   ├── CanLogout.php
│   │   └── CanResetPassword.php
│   ├── Models/
│   │   ├── Person.php
│   │   ├── User.php
│   │   ├── Admin.php
│   │   ├── Moderator.php
│   │   └── Guest.php
│   └── Services/
│       └── AuthService.php

🧠 SOLID Principles - توضیح و پیاده‌سازی

✅ 1. Single Responsibility Principle (SRP)

هر کلاس فقط باید یک مسئولیت مشخص داشته باشد.

پیاده‌سازی:

  • User فقط برای مدیریت اطلاعات کاربر.
  • Admin برای افزودن نقش به کاربر.
  • AuthService فقط مسئول login/authenticate است.

✅ 2. Open/Closed Principle (OCP)

برای توسعه باز و برای تغییر بسته باشد.

پیاده‌سازی:

  • کلاس پایه Person تعریف شده.
  • کلاس‌های User, Admin, Guest همگی از Person ارث‌بری می‌کنند بدون نیاز به تغییر کلاس پایه.

✅ 3. Liskov Substitution Principle (LSP)

اشیاء کلاس‌های فرزند باید قابل استفاده به جای والدشان باشند.

پیاده‌سازی:

تمام کلاس‌های User, Admin, Moderator, Guest می‌توانند به جای Person استفاده شوند و رفتار منطقی و صحیح داشته باشند.

✅ 4. Interface Segregation Principle (ISP)

بهتر است چند interface خاص داشته باشیم تا یک interface عمومی.

پیاده‌سازی:

تعریف چند interface به‌جای یک interface بزرگ:

  • CanLogin
  • CanLogout
  • CanResetPassword

هر کلاس فقط interfaceهایی را پیاده‌سازی می‌کند که واقعاً به آن نیاز دارد.

مثال:

  • User → تمام interfaceها
  • Guest → فقط CanLogout

✅ 5. Dependency Inversion Principle (DIP)

کلاس‌های سطح بالا نباید به کلاس‌های سطح پایین وابسته باشند، بلکه باید به abstraction وابسته باشند.

پیاده‌سازی:

use App\Interfaces\CanLogin;

class AuthService {
    public function __construct(private CanLogin $user) {}

    public function authenticate(): string {
        return $this->user->login();
    }
}

کلاس AuthService به جای وابستگی به کلاس User یا Admin، به interface CanLogin وابسته است.


👨‍🏫 کلاس‌های کلیدی

Person.php (Abstract Base Class)

abstract class Person {
    protected string $name;
    protected string $email;

    public function __construct(string $name, string $email = '') {
        $this->name = $name;
        $this->email = $email;
    }

    abstract public function getProfile(): string;
}

Interfaces

CanLogin.php

interface CanLogin {
    public function login(): string;
}

CanLogout.php

interface CanLogout {
    public function logout(): string;
}

CanResetPassword.php

interface CanResetPassword {
    public function resetPassword(): string;
}

User.php

class User extends Person implements CanLogin, CanResetPassword, CanLogout {
    public function __construct(string $name, string $email) {
        $this->name = $name;
        $this->setEmail($email);
    }

    public function setEmail(string $email): void {
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            throw new Exception('Email Not Valid');
        }
        $this->email = $email;
    }

    public function getProfile(): string {
        return "Name: {$this->name} - Email: {$this->email}";
    }

    public function login(): string {
        return "User [$this->name] is logged in.";
    }

    public function logout(): string {
        return "User [$this->name] logged out.";
    }

    public function resetPassword(): string {
        return "Password reset link sent to $this->email";
    }
}

Admin.php

class Admin extends User {
    private string $role;

    public function __construct(string $name, string $email, string $role) {
        parent::__construct($name, $email);
        $this->role = $role;
    }

    public function getProfile(): string {
        return "Name: {$this->name} - Email: {$this->email} - Role: {$this->role}";
    }

    public function isSuperAdmin(): bool {
        return $this->role === 'super-admin';
    }

    public function login(): string {
        return "Admin [$this->name] with role [$this->role] is logged in.";
    }
}

Guest.php

class Guest extends Person implements CanLogout {
    public function __construct(string $name) {
        parent::__construct($name);
    }

    public function getProfile(): string {
        return "Guest [$this->name]";
    }

    public function logout(): string {
        return "Guest [$this->name] logged out.";
    }
}

Moderator.php

class Moderator extends Person implements CanLogin {
    public function getProfile(): string {
        return "Name: {$this->name} - Email: {$this->email}";
    }

    public function login(): string {
        return "User [$this->name] is logged in.";
    }
}

AuthService.php

class AuthService {
    public function __construct(private CanLogin $user) {}

    public function authenticate(): string {
        return $this->user->login();
    }
}

🚀 اجرای پروژه

composer dump-autoload
php index.php

🧪 نمونه خروجی‌ها

User [Ali] is logged in.
Admin [Sara] with role [editor] is logged in.
Password reset link sent to ali@example.com
Guest [Visitor] logged out.

📌 اهداف آموزشی

  • تسلط عملی بر مفاهیم OOP در PHP
  • درک و پیاده‌سازی ۵ اصل مهم SOLID
  • آماده‌سازی برای توسعه معماری‌های بزرگ‌تر مثل:
    • Design Patterns
    • Clean Architecture
    • Domain-Driven Design (DDD)

👤 نویسنده

  • Reza
    رزومه‌ی آموزشی و نمونه‌کد برای نمایش در GitHub

📎 لایسنس

MIT License

About

PHP OOP & SOLID principles practice project

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages