PHP OOP — বাস্তব প্রয়োগ

লেসন ১৫ / ৩৯

Namespace

Namespace class-গুলোকে groups বা folders-এ ভাগ করে — একই নামের class একাধিক থাকতে পারে।

// App/Models/User.php
<?php
namespace App\Models;

class User {
    public $name;
}

// App/Services/User.php
<?php
namespace App\Services;

class User {
    public function login() { ... }
}

// index.php
<?php
use App\Models\User as UserModel;
use App\Services\User as UserService;

$u = new UserModel();
$s = new UserService();

// অথবা fully qualified
$u = new \App\Models\User();

Autoloading (PSR-4)

// Manual — প্রতিটি class include
require "User.php";
require "Product.php";
// বিরক্তিকর!

// Autoload দিয়ে
spl_autoload_register(function ($class) {
    $file = str_replace("\", "/", $class) . ".php";
    if (file_exists($file)) {
        require $file;
    }
});

// এখন
$u = new App\Models\User();
// PHP স্বয়ংক্রিয়ভাবে App/Models/User.php load করবে

Composer (PHP-র package manager) ⭐

# composer.json
{
    "autoload": {
        "psr-4": {
            "App\": "src/"
        }
    },
    "require": {
        "monolog/monolog": "^3.0",
        "phpmailer/phpmailer": "^6.8"
    }
}

# Install
composer install

// ব্যবহার
<?php
require "vendor/autoload.php";

use App\Models\User;
use Monolog\Logger;

$u = new User();

বাস্তব উদাহরণ: User Management System

<?php
// src/Models/User.php
namespace App\Models;

class User {
    public function __construct(
        public readonly int $id,
        public string $name,
        public string $email,
        private string $password
    ) {}

    public function verifyPassword(string $password): bool {
        return password_verify($password, $this->password);
    }

    public function toArray(): array {
        return [
            "id" => $this->id,
            "name" => $this->name,
            "email" => $this->email,
        ];
    }
}

// src/Services/AuthService.php
namespace App\Services;

use App\Models\User;
use App\Exceptions\AuthException;

class AuthService {
    private array $users = [];

    public function register(string $name, string $email, string $password): User {
        if ($this->findByEmail($email)) {
            throw new AuthException("ইমেইল ইতিমধ্যে ব্যবহৃত");
        }

        $user = new User(
            id: count($this->users) + 1,
            name: $name,
            email: $email,
            password: password_hash($password, PASSWORD_BCRYPT)
        );

        $this->users[] = $user;
        return $user;
    }

    public function login(string $email, string $password): User {
        $user = $this->findByEmail($email);
        if (!$user || !$user->verifyPassword($password)) {
            throw new AuthException("ভুল credentials");
        }
        return $user;
    }

    private function findByEmail(string $email): ?User {
        foreach ($this->users as $user) {
            if ($user->email === $email) return $user;
        }
        return null;
    }
}

// index.php
require "vendor/autoload.php";

use App\Services\AuthService;

$auth = new AuthService();

try {
    $user = $auth->register("করিম", "karim@example.com", "secret123");
    $loggedIn = $auth->login("karim@example.com", "secret123");
    echo json_encode($loggedIn->toArray());
} catch (\App\Exceptions\AuthException $e) {
    echo "ভুল: " . $e->getMessage();
}

SOLID Principles (সংক্ষেপে)

  • S — Single Responsibility — প্রতি class একটি কাজ
  • O — Open/Closed — extension খোলা, modification বন্ধ
  • L — Liskov Substitution — child class parent-কে প্রতিস্থাপন করতে পারবে
  • I — Interface Segregation — ছোট, focused interface
  • D — Dependency Inversion — concrete class-এর বদলে abstraction-এ নির্ভর