-
Hajipur, Bihar, 844101
In PHP, access modifiers are keywords that control the visibility and accessibility of class properties and methods. They are fundamental in Object-Oriented Programming (OOP) because they allow you to encapsulate data and restrict how objects are accessed or modified from outside the class. Understanding access modifiers is crucial for writing secure, maintainable, and modular PHP applications.
Access modifiers determine who can access a property or method in a class. PHP provides three types of access modifiers:
Public – Accessible from anywhere, including inside the class, subclasses, and outside objects.
Protected – Accessible within the class itself and any subclasses (child classes), but not from outside objects.
Private – Accessible only within the class where it is defined. It cannot be accessed by subclasses or external code.
Properties or methods declared as public can be accessed from anywhere, including outside the class.
Example:
class Car {
public $brand;
public function showBrand() {
echo "Car brand: " . $this->brand . "<br>";
}
}
$car = new Car();
$car->brand = "Toyota"; // Accessible outside
$car->showBrand(); // Accessible outside
Output:
Car brand: Toyota
Public access is useful for properties or methods that must be widely accessible, but it should be used carefully to prevent unwanted modification.
Protected properties or methods are accessible inside the class and its subclasses, but not from outside the object.
Example:
class Vehicle {
protected $type;
protected function setType($t) {
$this->type = $t;
}
}
class Bike extends Vehicle {
public function displayType($t) {
$this->setType($t); // Allowed, accessed from subclass
echo "Bike type: " . $this->type . "<br>";
}
}
$bike = new Bike();
$bike->displayType("Sport"); // Outputs: Bike type: Sport
// $bike->type = "Scooter"; // Error: Cannot access protected property
Protected access is ideal for properties and methods that should be shared with subclasses but hidden from external code.
Private properties or methods are accessible only within the class where they are declared. Subclasses or outside code cannot access them.
Example:
class BankAccount {
private $balance = 0;
private function setBalance($amount) {
$this->balance = $amount;
}
public function deposit($amount) {
$this->setBalance($this->balance + $amount);
}
public function getBalance() {
return $this->balance;
}
}
$account = new BankAccount();
$account->deposit(1000);
echo $account->getBalance(); // Outputs: 1000
// $account->balance = 500; // Error: Cannot access private property
Private access is useful for sensitive data that should not be modified directly, like account balances or user credentials.
Encapsulation – Protect internal data from unintended access or modification.
Security – Restrict access to sensitive information.
Maintainability – Makes code easier to modify without affecting external code.
Inheritance Control – Decide what properties or methods subclasses can access.
Methods can also have access modifiers:
Public methods can be called from anywhere.
Protected methods can be called only within the class or subclasses.
Private methods can only be called within the class itself.
Example:
class Calculator {
private function multiply($a, $b) {
return $a * $b;
}
public function calculateProduct($x, $y) {
return $this->multiply($x, $y);
}
}
$calc = new Calculator();
echo $calc->calculateProduct(5, 4); // Outputs: 20
// $calc->multiply(5, 4); // Error: Cannot access private method
Public properties/methods remain public in child classes.
Protected properties/methods remain protected but are accessible in subclasses.
Private properties/methods cannot be accessed directly by subclasses.
To allow a child class to modify a private property, use protected methods in the parent class to act as setters/getters.
Trying to access private or protected properties directly from outside the class.
Overusing public properties, which can lead to unintended modification.
Forgetting to provide getter/setter methods for private properties.
Changing access modifiers in child classes inconsistently, causing unexpected behavior.
Keep properties private and use public getters/setters to access them.
Use protected for properties/methods that should only be accessible by child classes.
Reserve public access for methods intended for external use.
Avoid mixing too many access modifiers without clear reason.
PHP provides public, protected, and private access modifiers.
Public – accessible from anywhere.
Protected – accessible inside class and subclasses.
Private – accessible only within the class.
Using access modifiers helps in encapsulation, security, and maintainability.
Proper use of access modifiers is a cornerstone of clean, reliable OOP code in PHP.
Mastering access modifiers allows you to write robust and secure classes, keeping internal details hidden while exposing only what is necessary.
Create a class Car with a private property speed and public methods setSpeed($s) and getSpeed(). Set and get the speed for an object.
Write a PHP program to create a class Student with a protected property grade. Create a subclass HighSchoolStudent that sets and displays the grade.
Create a class BankAccount with a private balance and public methods deposit($amount) and getBalance(). Demonstrate controlled access to the balance.
Create a class Employee with private properties name and salary, and public methods to set and get these values. Instantiate multiple employees and display their details.
Write a PHP program to create a class Vehicle with a protected method startEngine(). Create a subclass Bike and call startEngine() from it.
Create a class Book with a private property price and a public method setPrice($p) to modify it. Try accessing the property directly from outside the class and explain the result.
Create a class Calculator with private methods for addition, subtraction, multiplication, and division. Provide public methods that use these private methods to perform calculations.
Write a class Person with public property name and private property age. Add a public method display() that prints both. Instantiate an object and display the information.
Create a class ParentClass with a protected property value. Create a subclass ChildClass that sets and displays this value using a method.
Write a PHP program to create a class Company with private property employeeCount and public methods hireEmployee() and getEmployeeCount(). Demonstrate proper encapsulation by modifying and accessing the property only via methods.