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
<?php

class Player {
    // Private attributes
    private $health;
    private $armor;
    private $stamina;

    // Constructor
    public function __construct($health, $armor, $stamina) {
        $this->health = $health;
        $this->armor = $armor;
        $this->stamina = $stamina;
    }

    // Public methods
    public function receiveDamage($damage) {
        $this->health -= $damage; // Reduce health
    }

    public function shieldHit($armorDecrease) {
        $this->armor -= $armorDecrease; // Decrease armor
    }

    public function restoreHealth($healthIncrease) {
        $this->health += $healthIncrease; // Restore health
    }
}

// Create an instance of the Player class
$player = new Player(100, 50, 77);
$player->receiveDamage(10);
$player->shieldHit(5);
$player->restoreHealth(15);
?>

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
<?php

class PrivateExample {
    public $publicAttribute; // Public attribute
    private $privateAttribute; // Private attribute

    public function __construct() {
        $this->publicAttribute = "Public";
        $this->privateAttribute = "Private";
    }

    // Method to print attributes (for demonstration purposes)
    public function printAttributes() {
        echo "Public Attribute: " . $this->publicAttribute . "\n";
        // This line is fine inside the class method
        echo "Private Attribute: " . $this->privateAttribute . "\n";
    }
}

$example = new PrivateExample();
$example->printAttributes();
// This line is fine because publicAttribute is public
echo $example->publicAttribute . "\n";
// Uncommenting the following line would cause an error because privateAttribute is private
// echo $example->privateAttribute . "\n";
?>

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

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal