Courses Job Ready Fresher Training AI for Class 7 to 12 Corporate Training Placements Tutorials
Free Learning Resources

IT Tutorials & Interview Prep

Free guides, interview Q&As, and job responsibility breakdowns — curated by industry veterans to help you crack MNC interviews

109+
Tutorial Articles
9
Topic Categories
100%
Free to Read
← Back to PHP

Access Modifiers

PHP Last Updated: Oct 17, 2025

Access Modifiers

In the PHP programming language, every property and method of a class can have access modifiers which control their accessibility in the entire program.

There are three access modifiers in PHP:

  1. Public
  2. Protected
  3. Private

Below is a table for a better understanding of the access modifiers:

Class Member Access SpecifierAccess from own classAccessible from derived classAccessible by Object
PrivateYesNoNo
ProtectedYesYesNo
PublicYesYesYes

Public

Public property or method can be accessed by any code whether that code is inside or outside of that class. If a property or method is declared public, then it can be accessed anywhere from the code.

Example:

<?php
class Employee {
 public $name;
}

$emp1 = new Employee();
$emp1->name = 'Ganesh'; 
?>

Protected

Protected property or method can only be accessed within the class and by the classes derived from that class.

Example:

<?php
class ParentClass {
   protected $parentMsg = "protected parent attribute<br>";
   protected function parentDisplay() {
       echo "protected parent method<br>";
       echo $this->parentMsg;
   }
}
class ChildClass extends ParentClass {
   protected $childMsg = "Protected Child attribute.<br>";
   public function childDisplay() {
       echo "Public Child method to display protected parent members:<br>";
       $this->parentDisplay();
   }
}
$child = new ChildClass();
$child->childDisplay();
?>

Output:

Public Child method to display protected parent members:

protected parent method

protected parent attribute

Private

The Private property or method can only be accessed within the class. If it is called outside of the class, then it will throw an error.

Example:

<?php
class Employee {
 public $salary;

 private function set_salary($n) { // a private function
   $this->salary = $n;
 }
}

$emp1 = new Employee();
$emp1->set_salary('300'); // It will throw error as It cannot Access the Private Function
?>

Want to practice these scenarios in a real IT environment? Our 45-Day Internship Program gives you hands-on experience with Active Directory, Ticketing Tools, and Networking.
Join the Internship →