این پروژه تمرینی با هدف درک و پیادهسازی اصول OOP و SOLID در زبان PHP طراحی شده است. تمام اصول به صورت گامبهگام با مثالهای عملی پیادهسازی شدهاند.
- 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
هر کلاس فقط باید یک مسئولیت مشخص داشته باشد.
پیادهسازی:
Userفقط برای مدیریت اطلاعات کاربر.Adminبرای افزودن نقش به کاربر.AuthServiceفقط مسئول login/authenticate است.
برای توسعه باز و برای تغییر بسته باشد.
پیادهسازی:
- کلاس پایه
Personتعریف شده. - کلاسهای
User,Admin,Guestهمگی ازPersonارثبری میکنند بدون نیاز به تغییر کلاس پایه.
اشیاء کلاسهای فرزند باید قابل استفاده به جای والدشان باشند.
پیادهسازی:
تمام کلاسهای User, Admin, Moderator, Guest میتوانند به جای Person استفاده شوند و رفتار منطقی و صحیح داشته باشند.
بهتر است چند interface خاص داشته باشیم تا یک interface عمومی.
پیادهسازی:
تعریف چند interface بهجای یک interface بزرگ:
CanLoginCanLogoutCanResetPassword
هر کلاس فقط interfaceهایی را پیادهسازی میکند که واقعاً به آن نیاز دارد.
مثال:
User→ تمام interfaceهاGuest→ فقطCanLogout
کلاسهای سطح بالا نباید به کلاسهای سطح پایین وابسته باشند، بلکه باید به 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 وابسته است.
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;
}CanLogin.php
interface CanLogin {
public function login(): string;
}CanLogout.php
interface CanLogout {
public function logout(): string;
}CanResetPassword.php
interface CanResetPassword {
public function resetPassword(): string;
}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";
}
}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.";
}
}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.";
}
}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.";
}
}class AuthService {
public function __construct(private CanLogin $user) {}
public function authenticate(): string {
return $this->user->login();
}
}composer dump-autoload
php index.phpUser [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