Menu
Coddy logo textTech

Public, Protected, Private

Part of the Object Oriented Programming section of Coddy's PHP journey — lesson 38 of 91.

Welcome to the Encapsulation chapter! Encapsulation is about controlling access to your class's internal data. PHP provides three access modifiers that determine where properties and methods can be accessed from: public, protected, and private.

Here's how each modifier works:

<?php
class BankAccount {
    public $accountHolder;    // Accessible from anywhere
    protected $accountNumber; // Accessible in this class and child classes
    private $balance;         // Accessible only in this class
    
    public function __construct($holder, $number, $balance) {
        $this->accountHolder = $holder;
        $this->accountNumber = $number;
        $this->balance = $balance;
    }
    
    public function getBalance() {
        return $this->balance;
    }
}

$account = new BankAccount("John", "12345", 1000);

echo $account->accountHolder . "\n";  // Works - public
echo $account->getBalance();          // Works - public method

Output:

John
1000

Trying to access $account->balance directly would cause an error because it's private. The same applies to $account->accountNumber from outside the class hierarchy. This protection ensures that sensitive data can only be modified through controlled methods.

ModifierSame ClassChild ClassOutside
publicYesYesYes
protectedYesYesNo
privateYesNoNo

Key Point: Use private for internal data that shouldn't be accessed directly, protected for data that child classes need, and public only for safe external access.

challenge icon

Challenge

Easy

Let's build a secure employee management system that demonstrates how access modifiers protect sensitive data. You'll create a class hierarchy where different levels of information are accessible depending on where the code is running — inside the class, in a child class, or from outside.

You'll organize your code across three files:

  • Employee.php — Create an Employee class that manages employee data with appropriate visibility levels. The class should have:
    • A public property $name for the employee's name (safe for anyone to see)
    • A protected property $employeeId for the internal ID (needed by child classes)
    • A private property $salary for sensitive pay information (only this class should access it directly)
    The constructor should accept all three values and assign them. Add a public method getSalary() that returns the private salary — this is the controlled way to access protected data. Also add a public method getPublicInfo() that returns "[name] works here".
  • Manager.php — Create a Manager class that extends Employee. Include the Employee file. The constructor should accept name, employeeId, salary, and a department name. Call the parent constructor with the first three values, then store the department in a private property. Add a public method getTeamInfo() that returns "Manager [name] (ID: [employeeId]) leads [department]". Notice how the child class can access the protected $employeeId but would need getSalary() to access the private salary.
  • main.php — Include the Manager file. You'll receive four inputs: a name, an employee ID, a salary (as a string you'll convert to an integer), and a department name. Create a Manager instance with these values. Print three lines:
    • The manager's name accessed directly (public property)
    • The result of calling getTeamInfo()
    • The result of calling getSalary()

This challenge shows encapsulation in action: the name is freely accessible, the employee ID is shared with child classes through protected visibility, and the salary remains locked away in the parent class — accessible only through a controlled public method.

Cheat sheet

PHP provides three access modifiers to control where properties and methods can be accessed:

ModifierSame ClassChild ClassOutside
publicYesYesYes
protectedYesYesNo
privateYesNoNo

Example:

<?php
class BankAccount {
    public $accountHolder;    // Accessible from anywhere
    protected $accountNumber; // Accessible in this class and child classes
    private $balance;         // Accessible only in this class
    
    public function __construct($holder, $number, $balance) {
        $this->accountHolder = $holder;
        $this->accountNumber = $number;
        $this->balance = $balance;
    }
    
    public function getBalance() {
        return $this->balance;
    }
}

$account = new BankAccount("John", "12345", 1000);
echo $account->accountHolder;  // Works - public
echo $account->getBalance();   // Works - public method

Best Practices:

  • Use private for internal data that shouldn't be accessed directly
  • Use protected for data that child classes need
  • Use public only for safe external access

Try it yourself

<?php
require_once 'Manager.php';

// Read inputs
$name = trim(fgets(STDIN));
$employeeId = trim(fgets(STDIN));
$salary = intval(trim(fgets(STDIN)));
$department = trim(fgets(STDIN));

// TODO: Create a Manager instance with the input values

// TODO: Print the manager's name (accessed directly as public property)

// TODO: Print the result of calling getTeamInfo()

// TODO: Print the result of calling getSalary()
?>
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Object Oriented Programming