> For the complete documentation index, see [llms.txt](https://mominur.gitbook.io/mominur-notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://mominur.gitbook.io/mominur-notes/system-design/oop/encapsulation.md).

# Encapsulation

ধরো, তুমি একটা **Smartphone** কিনেছ। এই ফোনে অনেক কিছু আছে—processor, battery, camera, apps, এবং sensors। তুমি **phone** ব্যবহার করছো, কিন্তু তুমি phone-এর ভিতরের কাজের বিস্তারিত জানো না। তুমি শুধু ফোনকে নির্দেশ দিচ্ছো—call করো, message পাঠাও, camera open করো।

* এখানে **Phone** হলো **class**।
* Phone-এর ভিতরের hardware এবং logic হলো **data + methods**।
* তুমি যেসব button বা touchscreen ব্যবহার করছো তা হলো **public interface**।
* Phone-এর ভিতরের processor, memory, battery management system হলো **private data**।
* তুমি সরাসরি processor বা battery modify করতে পারো না—এটাই হলো **Encapsulation**।

সহজ কথায়: **Encapsulation হলো data + behavior কে hide করে, এবং controlled access দেয়া।**

***

### Encapsulation কি?

**Encapsulation** হলো OOP-এর একটি concept যা **data (properties) এবং methods** কে **একটি single unit বা class-এর মধ্যে** রাখে এবং **প্রয়োজন অনুযায়ী access control** দেয়।

* **Data hiding:** Internal details লুকানো।
* **Access control:** শুধু authorized methods বা interface থেকে data access করা যায়।

***

### Encapsulation কেন প্রয়োজন?

1. **Data security** – বাইরের কেউ class-এর data directly change করতে পারবে না।
2. **Controlled access** – তুমি নির্দিষ্ট নিয়ম অনুযায়ী data update করতে পারো।
3. **Maintainability** – Class structure পরিবর্তন হলেও বাইরের code break হয় না।
4. **Flexibility** – ভবিষ্যতে logic পরিবর্তন সহজ।

***

### **Encapsulation implement করার বিভিন্ন উপায়**

#### **1. Private Properties + Public Getter/Setter Methods**

**মূল ধারণা:**

* Property কে **private** করে বাইরের access block করা।
* Public method (getter/setter) দিয়ে controlled access দেয়া।

**Example:**

{% code lineNumbers="true" %}

```php
<?php
class Employee {
    private $name;
    private $salary;

    public function __construct($name, $salary) {
        $this->name = $name;
        $this->salary = $salary;
    }

    // Getter for name
    public function getName() {
        return $this->name;
    }

    // Setter for name
    public function setName($name) {
        $this->name = $name;
    }

    // Getter for salary
    public function getSalary() {
        return $this->salary;
    }

    // Setter for salary with validation
    public function setSalary($salary) {
        if($salary > 0) {
            $this->salary = $salary;
        } else {
            echo "Invalid salary value.\n";
        }
    }
}

// Usage
$emp = new Employee("Rahim", 50000);
$emp->setSalary(55000);
echo "Employee Salary: " . $emp->getSalary();
?>
```

{% endcode %}

**Benefit:**

* Data hide করা হলো।
* Validation দেওয়া সম্ভব।
* Direct modification prevent হলো।

***

#### **2. Protected Properties + Access in Child Class**

**মূল ধারণা:**

* Property কে **protected** করলে শুধু **child classes** access করতে পারে।
* Outside code direct access করতে পারবে না।

**Example:**

{% code lineNumbers="true" %}

```php
<?php
class Vehicle {
    protected $speed;

    public function setSpeed($speed) {
        if($speed >= 0) {
            $this->speed = $speed;
        }
    }

    public function getSpeed() {
        return $this->speed;
    }
}

class Car extends Vehicle {
    public function showSpeed() {
        echo "Car Speed: " . $this->speed;
    }
}

// Usage
$car = new Car();
$car->setSpeed(100);
$car->showSpeed();
?>
```

{% endcode %}

**Benefit:**

* Encapsulation + Inheritance possible।
* Child class behavior customize করতে পারবে।

***

#### **3. Read-only Properties (Getter Only)**

**মূল ধারণা:**

* শুধুমাত্র **getter method** থাকবে।
* Property read-only হবে।

**Example:**

{% code lineNumbers="true" %}

```php
<?php
class Circle {
    private $radius;

    public function __construct($radius) {
        $this->radius = $radius;
    }

    // Getter only
    public function getRadius() {
        return $this->radius;
    }

    // Calculate area without modifying radius
    public function getArea() {
        return pi() * $this->radius * $this->radius;
    }
}

// Usage
$circle = new Circle(5);
echo "Radius: " . $circle->getRadius();
echo "\nArea: " . $circle->getArea();
?>
```

{% endcode %}

**Benefit:**

* Data modification prevent করা।
* Safety + integrity বজায় রাখা।

***

#### **4. Write-only Properties (Setter Only)**

**মূল ধারণা:**

* শুধুমাত্র **setter method** থাকবে।
* Property externally read করা যাবে না।

**Example:**

{% code lineNumbers="true" %}

```php
<?php
class User {
    private $password;

    // Setter only
    public function setPassword($password) {
        if(strlen($password) >= 6) {
            $this->password = $password;
        } else {
            echo "Password too short.\n";
        }
    }

    // Internal use
    private function getPassword() {
        return $this->password;
    }
}

// Usage
$user = new User();
$user->setPassword("secret123");
// echo $user->getPassword(); // Not allowed, private
?>
```

{% endcode %}

**Benefit:**

* Sensitive data hide করা।
* Security enhanced।

***

#### **5. Using PHP Magic Methods (\_\_get and \_\_set)**

**মূল ধারণা:**

* Dynamic property access handle করতে পারি।
* Getter/setter define না করেও encapsulate করা যায়।

**Example:**

```php
<?php
class Product {
    private $data = [];

    public function __set($name, $value) {
        $this->data[$name] = $value;
    }

    public function __get($name) {
        return isset($this->data[$name]) ? $this->data[$name] : null;
    }
}

// Usage
$product = new Product();
$product->name = "Laptop";
$product->price = 45000;

echo "Product: " . $product->name;
echo "\nPrice: " . $product->price;
?>
```

**Benefit:**

* Dynamic properties handle possible।
* Still data encapsulated inside `$data` array।

***
