PHP - Encapsulation

Encapsulation is one of the fundamental principles of object-oriented programming (OOP). It refers to the bundling of data with the methods that operate on that data. In PHP, encapsulation can be achieved using classes and access modifiers such as public, private, and protected. This tutorial will guide you through the basics of encapsulation in PHP, starting with public members, then moving on to private members, and finally covering protected members.

PHP - Encapsulation

Public Members

Public members are accessible from anywhere, both inside and outside the class. They are declared using the public keyword. Here's an example of a simple class with a public member:

class Car {
    public $color;

    public function setColor($color) {
        $this->color = $color;
    }

    public function getColor() {
        return $this->color;
    }
}

$myCar = new Car();
$myCar->setColor("red");
echo "My car is " . $myCar->getColor() . "."; // Output: My car is red.

In this example, we have a Car class with a public property $color and two public methods setColor() and getColor(). We can easily set and get the color of the car from outside the class.

Private Members

Private members are only accessible within the class they are defined. They are declared using the private keyword. Here's an example of a class with a private member:

class BankAccount {
    private $balance;

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

    public function deposit($amount) {
        if ($amount > 0) {
            $this->balance += $amount;
        }
    }

    public function getBalance() {
        return $this->balance;
    }
}

$myAccount = new BankAccount(100);
$myAccount->deposit(50);
echo "My account balance is " . $myAccount->getBalance() . "."; // Output: My account balance is 150.

In this example, we have a BankAccount class with a private property $balance and two public methods deposit() and getBalance(). The $balance property is not directly accessible from outside the class, but we can manipulate it using the deposit() method and retrieve its value using the getBalance() method.

Protected Members

Protected members are accessible within the class they are defined and also within any subclasses. They are declared using the protected keyword. Here's an example of a class with a protected member:

class Animal {
    protected $name;

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

class Dog extends Animal {
    public function bark() {
        echo "Woof! I am " . $this->name . ".";
    }
}

$myDog = new Dog("Buddy");
$myDog->bark(); // Output: Woof! I am Buddy.

In this example, we have an Animal class with a protected property $name. The Dog class extends the Animal class and has access to the $name property because it is protected. We can use the $name property within the Dog class to implement the bark() method.

Conclusion

Encapsulation is a crucial concept in OOP that helps to maintain the integrity of data by restricting direct access to it. By using access modifiers like public, private, and protected, you can control how your data is accessed and modified. Understanding encapsulation will help you write more robust and maintainable code in PHP and other OOP languages.

Credits: Image by storyset