PHP Access Modifiers

Introduction

In Object-Oriented Programming (OOP), access control is crucial for protecting data and governing how class properties and methods are used. In PHP, Access Modifiers are used to define the visibility of class variables and methods.

πŸ“–
Important: Access modifiers determine where a property or method can be accessed from.

PHP provides three types of access modifiers:

  1. public
  2. protected
  3. private

These modifiers control access in three areas:

Modifier Within Class Outside Class Inheritance
Private Allowed Not Allowed Not Allowed
Protected Allowed Not Allowed Allowed
Public Allowed Allowed Allowed

Using access modifiers properly helps improve data security, encapsulation, and code organization.

For example, some class properties should be used only within the class, while others may be accessed by child classes or from outside the class.

Note: A class itself cannot be private or protected in PHP; only properties and methods can have access modifiers.

Syntax

The syntax for defining access modifiers in PHP is simple. You place the modifier before the variable or method.


class ClassName {

   public $variableName;

   protected $anotherVariable;

   private $privateVariable;

   public function methodName() {
       // code here
   }
}

Explanation:

  • public β†’ accessible everywhere
  • protected β†’ accessible within the class and child classes
  • private β†’ accessible only within the class

These modifiers help control how data and functions are accessed.

Example

Let’s start with a simple example using the private access modifier.


class Student {

   private $stu_name = "John";

   private function student_college_name() {
       return "HBIT Kanpur";
   }

   public function display_college_name() {
       return $this->stu_name . " is in " . $this->student_college_name();
   }

}

$stu_obj = new Student();

echo $stu_obj->display_college_name();

Output:

John is in HBIT Kanpur

In this example:

  • The property $stu_name is private
  • The method student_college_name() is also private.
  • Both can only be accessed inside the class

Real-world Example

Example 1:

Let’s look at a real-world example where a protected access modifier is used with inheritance.

Suppose we have a Student class and a Student Information child class.


<?php
class Student {

   protected $stu_name = "John";
   protected function student_college_name() {
       return "HBIT Kanpur";
   }
}

class StudentInformation extends Student {
   public function display_college_name() {
       return $this->stu_name . " is in " . $this->student_college_name();
   }
}

$stuinfo_obj = new StudentInformation();
echo $stuinfo_obj->display_college_name();
?>

Output:

John is in HBIT Kanpur

In this example:

  • The parent class contains protected members.
  • The child class can access those members.
  • But they cannot be accessed outside the class directly.

This is commonly used in large applications where child classes need controlled access to parent data.

Example 2:

For a public access modifier example, the property and method can be accessed directly outside the class.

Example:


<?php 
class Student {

   public $stu_name = "John";

   public function student_college_name() {
       return "HBIT Kanpur";
   }
}

$stu_obj = new Student();

echo $stu_obj->stu_name;
echo "
"; echo $stu_obj->student_college_name(); ?>

Output:

John
HBIT Kanpur

Explanation

Let’s understand how each access modifier works.

1. Private Access Modifier

The private modifier allows properties and methods to be accessed only within the same class.
Example:


private $stu_name;

This means:

  • Accessible inside the class βœ”
  • Not accessible outside the class βœ–
  • Not accessible in child classes βœ–

Private members are commonly used to protect sensitive data.

2. Protected Access Modifier

The protected modifier allows access inside the class and inside child classes.

Example:


protected $stu_name;

This means:

  1. Accessible inside the class βœ”
  2. Accessible in child classes βœ”
  3. Not accessible outside the class βœ–

Protected members are useful when working with inheritance.

3. Public Access Modifier

The public modifier allows access from anywhere in the program.

Example:


public $stu_name;

This means:

  • Accessible inside the class βœ”
  • Accessible outside the class βœ”
  • Accessible in child classes βœ”

Public members are typically used for functions that need to be called by other parts of the application.

PHP Access Modifiers – Interview Questions

Q 1: What are access modifiers?
Ans: They control access to class properties and methods.
Q 2: Types of access modifiers?
Ans: public, private, protected.
Q 3: What is public access?
Ans: Accessible from anywhere.
Q 4: What is private access?
Ans: Accessible only within the class.
Q 5: What is protected access?
Ans: Accessible within class and subclasses.

Conclusion

PHP access modifiers are an essential part of Object-Oriented Programming because they help control how class properties and methods are accessed. By using public, protected, and private modifiers, developers can secure data and organize code more effectively.

Private members protect sensitive data within the class, protected members allow controlled access to child classes, and public members provide open access across the program.

Using access modifiers properly improves code security, maintainability, and structure, especially in large applications. Understanding access modifiers is also important when working with advanced OOP concepts like inheritance, encapsulation, and polymorphism.

PHP Access Modifiers – Objective Questions (MCQs)

Q1. Which access modifier allows access only within the class?






Q2. Which access modifier allows access in the class and its subclasses?






Q3. Which access modifier allows access from anywhere?






Q4. Private properties cannot be accessed ______.






Q5. Access modifiers help in ______.






Related PHP Access Modifiers Topics