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:

TypesOverview
publicA public property or method can be accessed from anywhere, both inside and outside the class.
protectedA protected property or method can only be accessed from within the class and its subclasses. It cannot be accessed from outside the class.
privateA 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: 

<?php class Student { public $name; protected $age; private $gender; } $std1 = new Student(); $std1->name = 'Denis'; // OK $std1->age = 21; // ERROR $std1->age = 21; // ERROR ?>
This next example demonstrates how to add access modifiers to two methods of the same class. If you attempt to access either the setAge() or the setGender() functions in this case, you will get a fatal error (because the two functions are defined as protected and private functions, even though all of the properties are public):

Example: 

<?php class Student { public $name; public $age; public $gender; function setName($name) { // a public function (default) $this->name = $name; } protected function setAge($age) { // a protected function $this->age = $age; } private function setGender($gender) { // a private function $this->gender = $gender; } } $std = new Student(); $std->setName('Alex'); // OK $std->setAge(35); // ERROR $std->setAge(35); // ERROR ?>

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.

We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
1

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *