Welcome! Today's subject is Encapsulation, a cornerstone of Object-Oriented Programming (OOP). Encapsulation bundles data and the operations that we perform on them into one unit, namely an object. It guards data against unwanted alterations, ensuring the creation of robust and maintainable software.
Prepare yourself for an exciting journey as we delve into how encapsulation works and explore the vital role it plays in data privacy.
Starting with the basics, encapsulation is similar to packing data and the methods that modify this data into a single compartment known as a class
. It safeguards the data in an object from external interference.
To illustrate, consider a PHP class
representing a bank account. Without encapsulation, the account balance could be directly altered. With encapsulation, however, the balance can only change through specified methods, like depositing or withdrawing.
php1<?php 2 3class BankAccount { 4 public $balance; // no encapsulation 5 6 // Method to withdraw 7 public function withdraw($amount) { 8 $this->balance -= $amount; 9 } 10 11 // Method to deposit 12 public function deposit($amount) { 13 $this->balance += $amount; 14 } 15} 16 17$account = new BankAccount(); 18$account->balance += 1000; // directly accessing the balance 19 20?>
Encapsulation restricts direct access to an object's data and prevents unwanted data alteration. This principle is comparable to window blinds, allowing you to look out while preventing others from peeping in.
In encapsulation, private and public attributes are integral to data privacy. Private attributes, indicated by the private
keyword, require caution when being manipulated.
To illustrate, let's consider a PHP class
named Person
, which includes a private attribute name
.
php1<?php 2 3class Person { 4 // Private attribute 5 private $name; 6 7 // Constructor 8 public function __construct($name) { 9 $this->name = $name; 10 } 11 12 // Accessor method 13 public function getName() { 14 return $this->name; 15 } 16} 17 18$person = new Person("Alice"); 19echo $person->getName(); // Accessing private attribute via accessor method. Output: Alice 20// The following line would cause an error due to private access: 21// echo $person->name; 22 23?>
In this example, name
is private, and getName()
enables us to access name
. However, we don't provide a method to change the name
, preventing alterations.
To designate an attribute as private, we use the private
keyword.
Within encapsulation, PHP uses getter and setter methods to access or modify private attributes. In a class
, the getter method retrieves the attribute value, and the setter method alters it. Let's illustrate this.
php1<?php 2 3class Dog { 4 // Private attribute 5 private $name; 6 7 // Constructor 8 public function __construct($name) { 9 $this->name = $name; 10 } 11 12 // Setter method 13 public function setName($name) { 14 $this->name = $name; 15 } 16 17 // Getter method 18 public function getName() { 19 return $this->name; 20 } 21} 22 23$myDog = new Dog("Max"); 24$myDog->setName("Buddy"); 25echo $myDog->getName(); // Output: Buddy 26 27?>
Here, setName()
and getName()
serve as the setter and getter methods, respectively, for the private attribute name
.
Let's apply the principle of encapsulation to our BankAccount
class, which includes private attributes like an account number and balance, along with public methods for withdrawals, deposits, and balance checks.
php1<?php 2 3class BankAccount { 4 // Private attributes 5 private $accountNo; 6 private $balance; 7 8 // Constructor 9 public function __construct($accountNo, $balance) { 10 $this->accountNo = $accountNo; 11 $this->balance = $balance; 12 } 13 14 // Method to withdraw money 15 public function withdraw($amount) { 16 if ($amount > 0 && $amount <= $this->balance) { 17 $this->balance -= $amount; 18 } else { 19 echo "Invalid amount or insufficient balance."; 20 } 21 } 22 23 // Method to deposit money 24 public function deposit($amount) { 25 if ($amount > 0) { 26 $this->balance += $amount; 27 } else { 28 echo "Invalid deposit amount."; 29 } 30 } 31 32 // Method to check balance 33 public function checkBalance() { 34 return $this->balance; 35 } 36} 37 38$account = new BankAccount(1, 500.0); 39$account->withdraw(100); 40$account->deposit(50); 41echo $account->checkBalance(); // Prints: 450.0 42 43?>
In the above code, the BankAccount
class encapsulates account details that we would like to be hidden from the outer scope, and the public methods manipulate the balance in a controlled way. This way, we limit the potential interaction with the account's balance and other private information, improving security.
Admirable! Now it's your turn to apply what you've learned by practicing encapsulation in PHP. Next, we will have some practice exercises. Are you ready?
Remember, practice enhances your comprehension. Enjoy coding!