← Back to PHP Course | Chapter 8: OOP | Lesson 4 of 14

PHP Access Modifiers

Access modifiers are like doors with different locks: public is open to everyone, protected only to family, and private just to the object itself. They keep important things safe from accidental changes.
Syntax
php
class ClassName {
    public $public_property;       // accessible everywhere
    protected $protected_property; // class and subclasses
    private $private_property;     // this class only
}

Introduction to Access Modifiers

public, protected, and private control which code is allowed to read or modify a property or call a method, letting a class expose a clean interface while hiding its internal implementation details.

Example: Introduction to Access Modifiers

php
<?php
// Define the class `BankAccount`
class BankAccount {
    // Declare the private property `$balance`, set to `100`
    private $balance = 100;
    // Define the function `getBalance` with no parameters
    public function getBalance() {
        // Return `$this->balance` from this function
        return $this->balance;
    }
}
// Create a new `BankAccount` instance, stored in `$account`
$account = new BankAccount();
// Print `$account->getBalance()` to the output
echo $account->getBalance();
?>

The Public Modifier

public members are accessible from anywhere, including outside the class entirely, and are the default visibility PHP uses if you omit a modifier — though explicitly stating it is considered better practice.

Example: The Public Modifier

php
<?php
// Define the class `Car`
class Car {
    // Declare the public property `$color`, set to "red"
    public $color = "red";
}
// Create a new `Car` instance, stored in `$car`
$car = new Car();
// Print `$car->color` to the output
echo $car->color;
?>

The Private Modifier

private members are accessible only from within the exact class that defines them, not even from a subclass, which is how you truly hide internal state that no other code (including child classes) should touch directly.

Example: The Private Modifier

php
<?php
// Define the class `Account`
class Account {
    // Declare the private property `$pin`, set to "1234"
    private $pin = "1234";
    // Define the function `checkPin` taking `$input`
    function checkPin($input) {
        // Return `$input === $this->pin` from this function
        return $input === $this->pin;
    }
}
// Create a new `Account` instance, stored in `$account`
$account = new Account();
// Print a detailed dump (with types) of `$account->checkPin("1234")`
var_dump($account->checkPin("1234"));
?>

The Protected Modifier

protected members sit in between: accessible from the defining class and any class that extends it, but not from outside code, which is the right choice for internal details a subclass legitimately needs to build on.

Example: The Protected Modifier

php
<?php
// Define the class `Animal`
class Animal {
    // Declare the protected property `$sound`, set to "..."
    protected $sound = "...";
}
// Define the class `Dog`, inheriting from `Animal`
class Dog extends Animal {
    // Define the function `makeSound` with no parameters
    function makeSound() {
        // Print `$this->sound` to the output
        echo $this->sound;
    }
}
// Create a new `Dog` instance, stored in `$dog`
$dog = new Dog();
$dog->makeSound();
?>

Getter and Setter Methods

Restricting properties to private or protected and exposing controlled access through public methods (getters/setters) is a core encapsulation technique that prevents external code from putting an object into an invalid state.

Example: Getter and Setter Methods

php
<?php
// Define the class `Account`
class Account {
    // Declare the private property `$balance`, set to `0`
    private $balance = 0;
    // Define the function `setBalance` taking `$amount`
    function setBalance($amount) {
        // Check whether `$amount >= 0`
        if ($amount >= 0) {
            $this->balance = $amount;
        }
    }
    // Define the function `getBalance` with no parameters
    function getBalance() {
        // Return `$this->balance` from this function
        return $this->balance;
    }
}
// Create a new `Account` instance, stored in `$account`
$account = new Account();
$account->setBalance(500);
// Print `$account->getBalance()` to the output
echo $account->getBalance();
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Accessing a private property from outside the class, which throws an error.
  2. Using private in a parent class and expecting child classes to access it, when they need protected.
  3. Leaving out an access modifier on a property and expecting it to be private, when properties need a modifier and methods default to public.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.