Lesson 3
Encapsulation and Privacy in PHP Object-Oriented Programming
Lesson Overview

Hello! In this lesson, we're revisiting Encapsulation, Private Attributes, and Private Methods in Object-Oriented Programming (OOP). Imagine encapsulation as an invisible fence safeguarding a garden from outside interference, keeping data and methods safe within. Within this garden, certain plants (Private Attributes and Private Methods) are only for the gardener's eyes. These are crucial for making your classes more robust and secure!

Into the Encapsulation

Encapsulation in OOP wraps up data and methods into a class. This organizational approach tidies the code and reinforces security. If you were to code a multiplayer game, for example, you could create a Player class, encapsulating data (health, armor, stamina) and methods (receiveDamage, shieldHit, restoreHealth).

php
1<?php 2 3class Player { 4 // Private attributes 5 private $health; 6 private $armor; 7 private $stamina; 8 9 // Constructor 10 public function __construct($health, $armor, $stamina) { 11 $this->health = $health; 12 $this->armor = $armor; 13 $this->stamina = $stamina; 14 } 15 16 // Public methods 17 public function receiveDamage($damage) { 18 $this->health -= $damage; // Reduce health 19 } 20 21 public function shieldHit($armorDecrease) { 22 $this->armor -= $armorDecrease; // Decrease armor 23 } 24 25 public function restoreHealth($healthIncrease) { 26 $this->health += $healthIncrease; // Restore health 27 } 28} 29 30// Create an instance of the Player class 31$player = new Player(100, 50, 77); 32$player->receiveDamage(10); 33$player->shieldHit(5); 34$player->restoreHealth(15); 35?>

Now, $player is an instance of the Player class on which you can call methods.

Remark the Privacy

In PHP, the private access specifier designates attributes or methods as private. Private members are only accessible within the same class. Unlike some other languages, PHP class members are public by default.

php
1<?php 2 3class PrivateExample { 4 public $publicAttribute; // Public attribute 5 private $privateAttribute; // Private attribute 6 7 public function __construct() { 8 $this->publicAttribute = "Public"; 9 $this->privateAttribute = "Private"; 10 } 11 12 // Method to print attributes (for demonstration purposes) 13 public function printAttributes() { 14 echo "Public Attribute: " . $this->publicAttribute . "\n"; 15 // This line is fine inside the class method 16 echo "Private Attribute: " . $this->privateAttribute . "\n"; 17 } 18} 19 20$example = new PrivateExample(); 21$example->printAttributes(); 22// This line is fine because publicAttribute is public 23echo $example->publicAttribute . "\n"; 24// Uncommenting the following line would cause an error because privateAttribute is private 25// echo $example->privateAttribute . "\n"; 26?>

Private attributes and methods are inaccessible directly from an instance in non-member functions. This arrangement helps maintain integrity.

Private Attributes

Private Attributes, which can only be altered via class methods, limit outside interference. For instance, a BankAccount class might feature a balance private attribute that one could change only through deposits or withdrawals.

php
1<?php 2 3class BankAccount { 4 private $accountNumber; 5 private $balance; 6 7 // Constructor 8 public function __construct($accountNumber, $balance) { 9 $this->accountNumber = $accountNumber; 10 $this->balance = $balance; 11 } 12 13 // Public method to deposit money 14 public function deposit($amount) { 15 $this->balance += $amount; // Deposit money 16 } 17 18 // Method to print balance (for demonstration purposes) 19 public function printBalance() { 20 echo "Balance: " . $this->balance . "\n"; 21 } 22} 23 24$bankAccount = new BankAccount(1234, 100.0); 25$bankAccount->printBalance(); 26// Uncommenting the following line would cause an error because balance is private 27// echo $bankAccount->balance . "\n"; 28?>

Here, $balance is private, thus ensuring the integrity of the account balance.

Private Methods

Like private attributes, private methods are accessible only within their class. Here's an example:

php
1<?php 2 3class BankAccount { 4 private $accountNumber; 5 private $balance; 6 7 // Constructor 8 public function __construct($accountNumber, $balance) { 9 $this->accountNumber = $accountNumber; 10 $this->balance = $balance; 11 } 12 13 // Public method calling the private method 14 public function addYearlyInterest() { 15 $this->addInterest(0.02); // Adds 2% interest 16 } 17 18 // Method to print balance (for demonstration purposes) 19 public function printBalance() { 20 echo "Balance: " . $this->balance . "\n"; 21 } 22 23 // Private method 24 private function addInterest($interestRate) { 25 $this->balance += $this->balance * $interestRate; // Calculation of interest 26 } 27} 28 29$bankAccount = new BankAccount(1234, 100.0); 30$bankAccount->addYearlyInterest(); 31$bankAccount->printBalance(); 32// Uncommenting the following line would cause an error because addInterest is private 33// $bankAccount->addInterest(0.1); 34?>

Here, addYearlyInterest is a public method that calls the private method addInterest.

Lesson Summary

Great job refreshing your understanding of encapsulation, private attributes, and private methods concepts in PHP! Correctly understanding and applying these foundational principles of OOP make your code concise, robust, and secure.

Coming up next is hands-on practice. Keep up the good work — exciting exercises are just around the corner!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.