PHP Access Modifiers
In this page:
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
// 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();
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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;
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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"));
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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();
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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();
?>
Login to try C/C++/Java/PHP code in the editor
- Accessing a
privateproperty from outside the class, which throws an error. - Using
privatein a parent class and expecting child classes to access it, when they needprotected. - Leaving out an access modifier on a property and expecting it to be private, when properties need a modifier and methods default to public.
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: