Object-Oriented Programming (OOP) has become an essential part of modern PHP development, enabling developers to write cleaner, reusable, and more modular code. One of the core concepts of OOP in PHP is the use of Access Specifiers, which help control how and where the properties and methods of a class can be accessed.
Access specifiers, also known as visibility modifiers, define the scope of accessibility for class properties and methods. In PHP, there are three main types of access specifiers: public
, protected
, and private
.
In this blog, we will explore these access specifiers, understand how they work, and see examples using PHP 8+ syntax.
Public Access Specifier
The public
access specifier allows a property or method to be accessible from anywhere—both inside and outside the class.
Example
class Car {
public string $brand;
public function __construct(string $brand) {
$this->brand = $brand;
}
public function getBrand(): string {
return $this->brand;
}
}
$car = new Car('Tesla');
echo $car->getBrand(); // Output: Tesla
Explanation:
- The property
$brand
and methodgetBrand()
are declared aspublic
, which means they can be accessed both inside and outside theCar
class. - The
getBrand()
method is called from an external scope and works fine since it's public.
Private Access Specifier
The private
access specifier restricts the access to properties and methods within the same class. They cannot be accessed directly from outside the class, not even by derived (child) classes.
Example:
class Car {
private string $model;
public function __construct(string $model) {
$this->model = $model;
}
private function getModel(): string {
return $this->model;
}
public function showModel(): string {
return $this->getModel();
}
}
$car = new Car('Model S');
// echo $car->model; // This will throw an error: Cannot access private property
echo $car->showModel(); // Output: Model S
Explanation:
- The
$model
property and thegetModel()
method are declared asprivate
, making them inaccessible from outside the class. - Access to
$model
is only possible through theshowModel()
method, which ispublic
and internally calls theprivate
methodgetModel()
Protected Access Specifier
The protected
access specifier allows a property or method to be accessible only within the class itself and by any class that extends it (subclasses). However, it cannot be accessed directly from outside the class.
Example:
class Vehicle {
protected string $engine;
public function __construct(string $engine) {
$this->engine = $engine;
}
protected function getEngine(): string {
return $this->engine;
}
}
class Car extends Vehicle {
public function showEngine(): string {
return $this->getEngine();
}
}
$car = new Car('V8');
echo $car->showEngine(); // Output: V8
Explanation:
- The
$engine
property and thegetEngine()
method are declared asprotected
. This means that they cannot be accessed from outside the classVehicle
, but they can be accessed inside theCar
class, which extendsVehicle
. - The
showEngine()
method ispublic
, allowing access to the protectedgetEngine()
method from outside the class through inheritance.
Access Specifiers in Inheritance
Access specifiers play a crucial role in class inheritance. As mentioned earlier, private
members are not accessible by derived classes, while protected
members are accessible within child classes.
Example:
class Animal {
protected string $type;
public function __construct(string $type) {
$this->type = $type;
}
protected function getType(): string {
return $this->type;
}
}
class Dog extends Animal {
public function showType(): string {
return "This is a " . $this->getType();
}
}
$dog = new Dog('Mammal');
echo $dog->showType(); // Output: This is a Mammal
Explanation:
- The
type
property andgetType()
method in theAnimal
class are declared asprotected
. This means they can be accessed by theDog
class through inheritance. - The
showType()
method in theDog
class ispublic
, providing access to theprotected
methodgetType()
from the outside.
Access Specifiers Use Cases:
- Use
private
for properties and methods that are meant to be used only within the class. This helps encapsulate the internal logic and protect the class’s internal state from unintended modification. - Use
protected
when you want to allow child classes to interact with certain properties or methods, but still want to restrict access from outside the hierarchy. - Use
public
sparingly, only when you need to provide direct access to class members from outside the class. Overexposure of internal data can lead to poor encapsulation and maintainability issues.
Conclusion
Access specifiers are a key component of Object-Oriented Programming in PHP, providing control over how and where class properties and methods can be accessed. By leveraging public
, protected
, and private
correctly, developers can ensure better encapsulation, maintainability, and security in their PHP applications.
Whether you're building small applications or large-scale systems, understanding and applying access specifiers effectively will help you write cleaner, more maintainable code. And with PHP's latest versions offering strong typing and modern OOP features, you can take full advantage of these powerful tools to create robust and scalable applications.