Access modifiers are a very important part in PHP object-oriented programming (OOP). They determine the visibility and accessibility of properties and methods within a class. We can not define any method or property without Access modifiers. In this article, we will learn about Access Modifiers in PHP in detail with help of examples.
Overview of Access Modifiers in PHP
Access modifiers can be applied to properties and methods to control where they can be accessed. There are three types of access modifiers:
- Public: The property or method is public if it can be accessed from anywhere.
- Protected: The property or method can be accessed both within the class and within the subclass (a class that inherits the class).
- Private: The property or method is only accessible within the class.
Public
When you use the public keyword in front of a property or method, it becomes public. It means you can access the property and method from both within and outside of the class. The following example demonstrates the property’s public access modifier:
<?php class Color{ public $color; public function getColor() { return $this->color; } } ?>
The Color class in the above example has a public property ($colour) and a public method (getColor()). You can now access the property and method from both within and outside the class.
For Example
<?php class Color{ public $color; public function getColor() { return $this->color; } } $obj2 = new Color(); $obj2->color = 'Yellow'; echo "Color name is: ".$obj2->getColor(); ?>
Private
The private access modifier is used to block access to properties and methods from outside the class. The following example converts the Color class’s $color field from public to private:
For Example
<?php class Color{ private $color; public function getColor() { return $this->color; } } ?>
In the preceding example, we defined a private property colour and attempted to retrieve the colour name using the getColor() method. However, if you assign a colour name outside of the class, you will receive a Fatal error “Fatal error: Uncaught Error: Cannot access private property.” Because when you declare a field or method as Private, you cannot access it from outside the class.
When you want to gain access to private property, you must first define a public method for managing that private property. Typically, in order to gain access to private property, you must first define public method for managing private property:
Setter Method | It always assigns a new value to private property. |
Getter Method | It always returns the private property’s value. |
<?php class Color{ private $color; public function setColorName($color) { $this->color = $color; } public function getColorName() { return $this->color; } } ?>
In the above example, the Color class defines the setter (setColorName) method for changing the colour name and the getter (getColorName) method for displaying the colour name. Now, consider the following example to see how to set the colour name property and retrieve its value:
<?php $obj2 = new Color(); $obj2->setColorName('Yellow'); echo "Color name is: ".$obj2->getColorName().'<br/>'; ?>
You can restrict users from gaining direct access to the property from outside the class by utilizing the private property.
Furthermore, the getter and setter methods ensure that the property can only be accessed via these methods. Furthermore, the getter and setter methods can be used to implement custom logic to alter the property value.
Also read about PHP Object & Class
Protected
The protected properties and methods are accessible from within the class as well as any class that extends the class. To define a protected property and method, you prefix the property name with the protected keyword, as you would with the private and public access modifiers:
Syntax
<?php protected $propertyname; protected function getColorName(); ?>
For Example
<?php class Student{ protected $stname; public function __construct($stname) { $this->stname = $stname; } } class Profile extends Student{ public function getStudentName() { return ucwords($this->stname); } } ?>
In above example, we defined a protected property($stname), and use constructor to set student name, and when you want to get this student name in other class, then you have to extends this Student class in Profile class and define getStudentName() method to get Student name.
<?php $stobj = new Profile('Rohit Sharma'); echo "The Student name is: ".$stobj->getStudentName(); ?>
Now, see this below example for protected method,
<?php class Student{ protected $stname; public function __construct($stname) { $this->stname = $stname; } protected function getStudentNameFormat() { return ucwords($this->stname); } public function displayStudentName() { return $this->getStudentNameFormat(); } } class Profile extends Student{ protected function getStudentNameFormat() { return strtoupper($this->stname); } } ?>
In the preceding example, the Student class has the protected property $stname as well as the protected function getStudentNameFormat(). The $name is returned with the initial character of each word transformed to uppercase by the getStudentNameFormat() method.
The Student class also includes a public method called displayStudentName(), which calls the getStudentNameFormat() method to return the formatted name.
PHP access modifiers are an essential component of object-oriented programming that let you manage the accessibility and visibility of class methods and attributes. You may enforce encapsulation, safeguard your data, and build more reliable and stable programs by being aware of and utilizing public, protected, and private modifiers effectively. By describing how various portions of your code should interact, access modifiers when used properly not only increase the security and integrity of your code but also make it easier to read and maintain.