PHP Access Modifiers: Understanding Their Purpose and Implementation
The purpose of this article is to examine concepts of PHP access modifiers and their importance to object-oriented programming.
In object-oriented programming, access modifiers are used to control the visibility of class members (properties and methods).
Php access modifier on a class property or method determines which methods and properties in the class are visible to the user.
This lesson will discuss the differences between public, protected, and private properties and methods as well as how they are used in various situations.
PHP access modifiers can be classified into three types:
Types | Overview |
public | A public property or method can be accessed from anywhere, both inside and outside the class. |
protected | A protected property or method can only be accessed from within the class and its subclasses. It cannot be accessed from outside the class. |
private | A private property or method can only be accessed from within the class. It cannot be accessed from outside the class or its subclasses. |
PHP Access Modifiers
PHP access modifiers can be used on properties and methods to control where they are accessible (or not).
Here we have added three different access modifiers to each of the three properties in the following example.
The name property is public, so if you set it, it will work just fine.
You will get a fatal error if you try to make any changes to the color or the weight property, because you cannot set them (since the age and gender properties are protected and private).
Example: 
Example: 
Example Explanation
Above code is an example of an object-oriented programming concept called encapsulation. It defines a class called “Student” with three properties, $name, $age, and $gender, and three methods, setName(), setAge(), and setGender(), which are used to set the values of the properties.
The setName() method is a public method, meaning that it can be accessed from anywhere outside of the class, and it sets the value of the $name property.
The setAge() method is a protected method, meaning that it can only be accessed from within the class or its subclasses. It sets the value of the $age property.
The setGender() method is a private method, meaning that it can only be accessed from within the class itself. It sets the value of the $gender property.
When an object of the class is created using the “new” keyword, the properties are automatically initialized to null values.
Then, the setName() method is called on the object, which sets the value of the $name property to “Alex”.
However, when the setAge() method is called on the object, it throws an error because it is a protected method and cannot be accessed from outside the class.
In summary, encapsulation is a technique that allows the data and methods of a class to be hidden and accessed only through public interfaces. With the visibility set for methods, developers can control the access to properties and prevent unwanted modifications.
Purpose of PHP Access Modifiers
The primary purpose of PHP access modifiers is to control the visibility and accessibility of class members. This ensures that class members are only accessible from the appropriate locations, preventing unauthorized access or modification of class properties and methods.
Here are some specific purposes of each access modifier:
Public
Public members can be accessed from anywhere, making them suitable for properties and methods that need to be accessed from outside the class. For example, a getter method that returns the value of a private property can be made public.
Private
Private members are only accessible from within the class where they are defined, making them suitable for properties and methods that should not be accessed or modified from outside the class. For example, a private property that stores a password or other sensitive information should be made private.
Protected
Protected members are only accessible from within the class where they are defined and its subclasses, making them suitable for properties and methods that should only be accessed or modified by the class and its subclasses. For example, a protected method that performs a specific operation can be called by the class and its subclasses but not from outside.