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.
Unraveling Encapsulation
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.
PHP
<?phpclass BankAccount { public $balance; // no encapsulation // Method to withdraw public function withdraw($amount) { $this->balance -= $amount; } // Method to deposit public function deposit($amount) { $this->balance += $amount; }}$account = new BankAccount();$account->balance += 1000; // directly accessing the balance?>
Encapsulation: Guardian of Data Privacy
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal
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.
PHP
<?phpclass Person { // Private attribute private $name; // Constructor public function __construct($name) { $this->name = $name; } // Accessor method public function getName() { return $this->name; }}$person = new Person("Alice");echo $person->getName(); // Accessing private attribute via accessor method. Output: Alice// The following line would cause an error due to private access:// echo $person->name;?>
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.
Getter and Setter Methods in Encapsulation
Practical Application and Summary
Hands-on Exercise and Practice
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!
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.
PHP
<?phpclass Dog { // Private attribute private $name; // Constructor public function __construct($name) { $this->name = $name; } // Setter method public function setName($name) { $this->name = $name; } // Getter method public function getName() { return $this->name; }}$myDog = new Dog("Max");$myDog->setName("Buddy");echo $myDog->getName(); // Output: Buddy?>
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.
PHP
<?phpclass BankAccount { // Private attributes private $accountNo; private $balance; // Constructor public function __construct($accountNo, $balance) { $this->accountNo = $accountNo; $this->balance = $balance; } // Method to withdraw money public function withdraw($amount) { if ($amount > 0 && $amount <= $this->balance) { $this->balance -= $amount; } else { echo "Invalid amount or insufficient balance."; } } // Method to deposit money public function deposit($amount) { if ($amount > 0) { $this->balance += $amount; } else { echo "Invalid deposit amount."; } } // Method to check balance public function checkBalance() { return $this->balance; }}$account = new BankAccount(1, 500.0);$account->withdraw(100);$account->deposit(50);echo $account->checkBalance(); // Prints: 450.0?>
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.