php

What is the Difference Between Public, Private, and Protected in PHP

PHP is widely used for developing dynamic websites and web applications. PHP provides keywords known as access modifiers. The access modifier properties are used to access the variable, class, or class properties in three different ways, such as public, private, and protected. In this article, we will discuss these access modifiers and determine the difference between these three access modifiers in the PHP programming language.

What are Access Modifiers in PHP?

In PHP access modifiers are essential because they give users the ability to regulate the visibility as well as the accessibility of class attributes and methods. As a result of this, it provides encapsulation and data security, and hence, the class members are protected from unauthorized access or alteration. Also, it makes the maintenance of code and debugging easier.

Types of Access Modifiers

The three access modifiers that exist in PHP are discussed below:

  • Public: You can access a public method from outside the class. This implies that any code can access, change, and invoke a public method.
  • Private: To access the private method is restricted to the class itself. This implies that none of the other codes, even if it is a child class, is permitted to access a private method or read the private value.
  • Protected: A protected method is accessible only inside a class and classes that are related to it; it cannot be used outside of those classes. Any source code that needs to access the class instance can read or alter the data of a protected property or invoke a protected function.

Difference Between Public, Private, and Protected Access Modifiers in PHP

        Property             Public         Private         Protected
      Accessibility These access modifiers can be accessible anywhere such as inside and outside of the code. Private access specifiers are only accessible within the class. It could be accessible inside the class and its related(child) classes, and methods.
       Flexibility The most flexibility is offered by public resources as they can be accessed from anywhere. Since they are exclusively accessible within the class, these methods offer the least flexibility. The protected method provides moderate flexibility as child classes can access these methods.
      Encapsulation Other code may access or encapsulate public methods, but the programmer has control over this. A private method is encapsulated inside the class, making it impossible for outside code to access or update them. Its encapsulation process is the same as the public method in PHP.
         Security Public methods are accessible from anywhere. So, they provide a low level of security in the code. The maximum level of security is provided by private access modifiers because they are totally contained within the class. Since they can only be accessed by members of the class and its subclasses, protected access modifiers offer a reasonable level of security.

Let’s see the difference between these access specifiers using a simple program example in PHP.

Example: PHP Program with Private, Public, and Protected Access Modifiers

The following example demonstrates the difference between public, private, and protected in one example code:

<?php
class MyClass {
  public $public = "Public-variable.\n"; // Can be accessed from anywhere
  private $private = "Private-variable.\n"; // Can only be accessed from within the class
  protected $protected = "Protected-variable."; // Can be accessed within the class and any subclasses

  public function getPrivate() {
    return $this->private;
  }

  public function getProtected() {
    return $this->protected;
  }
}
$obj = new MyClass();

echo $obj->public;
echo $obj->getPrivate();
echo $obj->getProtected();
?>

 

In the above program, we defined a class as MyClass with a public property as $public which could access anywhere in code, a private property as $private, and a protected property called $protected, hence we cannot access private and protected specifiers directly from outside the class. As we used two public methods called getPrivate() and getProtected() that return the values of the private and protected properties variables, respectively, and the output is given below:

Conclusion

PHP offers many functionalities and one of them is the keywords called access modifiers. These access modifiers provide different access patterns to the data of classes in the PHP program. In the above tutorial, we have seen the difference between access modifiers in terms of accessibility, encapsulation, flexibility, and security that are offered in PHP.

About the author

Kaynat Asif

My passion to research new technologies has brought me here to write for the LinuxHint. My major focus is to write in C, C++, and other Computer Science related fields. My aim is to share my knowledge with other people.