Difference Between Private and Protected in PHP
Difference Between Private and Protected in PHP

Three Main Difference Between Private and Protected in PHP

Three Main Difference Between Private and Protected in PHP

In this article, we will discuss about two visibility modifiers in PHP. Which are Private and Protected. So let’s get started.

 

There are three access modifiers in PHP. Public, Private and Protected. In this article we will talk about Private and Protected as these two are bit confusing for beginners.

As we know already,  protected and private are two visibility modifiers used to control access to class properties and methods. Let’s learn them one by one. First “Protected”.

Protected

  • Level of Access: Properties and methods declared as protected can be accessed:
    • Within the class itself,
    • By classes derived from that class (subclasses),
    • By parent classes.
  • Use Case: We use protected when we want to allow child classes to access and possibly modify certain properties or methods of the parent class.
  • For Example:
    class ParentClass {
        protected $protectedProperty = "This is Protected Property. ";
    
        protected function protectedMethod() {
            echo "This is a Protected Method.";
        }
    }
    
    class ChildClass extends ParentClass {
        public function accessProtected() {
            echo $this->protectedProperty; // Accessing protected property of Parent Class
            $this->protectedMethod();      // Accessing protected method of Parent Class
        }
    }
    
    $child = new ChildClass();
    $child->accessProtected(); 
    
    
    // Output: This is Protected Property. This is a Protected Method.
    

    Private

    • Level of Access: Properties and methods declared as private can only be accessed within the class that defines them. They are not accessible from child classes or any other part of the code. It gives more security to the code and protect from outside modification.
    • Use Case: We use private when we want to completely hide certain properties or methods from any external access, including from subclasses.
    • For Example:
    • class ParentClass {
          private $privateProperty = "This is a Private Property. ";
      
          private function privateMethod() {
              echo "Private Method.";
          }
      
          public function accessPrivate() {
              echo $this->privateProperty; // Accessing private property within the class
              $this->privateMethod();      // Accessing private method within the class
          }
      }
      
      class ChildClass extends ParentClass {
          public function accessPrivate() {
              // Below lines will cause error because private members cannot be accessed in child classes.
              // echo $this->privateProperty;
              // $this->privateMethod();
          }
      }
      
      $parent = new ParentClass();
      $parent->accessPrivate(); // Output: This is a Private Property. Private Method.
      

      So the main differences are:

      1. Visibility level:
        • protected: Accessible within the class, by child classes, and by parent classes.
        • private: Accessible only within the class that declares it.
      2. Inheritance aspect:
        • protected: Allows inheritance. Child classes can access protected members of the parent class.
        • private: Does not allow inheritance. Child classes cannot access private members of the parent class.
      3. Encapsulation:
        • protected: Provides a balance between encapsulation and inheritance. It hides data from the outside world but allows child classes to access it.
        • private: Provides strict encapsulation. It hides data from both the outside world and child classes.

When to Use

  • Use protected when we need to share properties or methods with derived classes but want to keep them hidden from external code.
  • Use private when we want to enforce strict encapsulation and ensure that properties or methods are not accessible or modifiable from outside the class.

 

Editorial Staff

A Learner and trying to share what I learned!