Access Modifiers

In this blog page, you will learn about PHP access modifiers and understand the differences between them.



Access modifiers (or access specifiers) are keywords in object-oriented languages that set the accessibility of classes, methods, and other members

 

In PHP Oops we have three predefined  Access modifiers (keywords) such as Public, Private, and Protected.

 

Public – By Using this access modifier, class members will be publicly accessible from anywhere, even from outside the scope of the class. by default the classes are Public



Private – BY using this keyword, class members will be accessed within the class itself. It protects members from outside class access with the reference of the class instance.

Protected – This is the same as private modifier, with one exception, the class members defined as protected can still be accessed from its subclass or child class.

 



PHP – Access Modifiers in Oops With Example

Public & private Access Modifier:-

 

Protected Access modifier

Protected members can only be accessible inside the derived class or child class. See the below example.

The above example $objpar is the object of the parent class and $x is the protected member variable, So it can not be accessed directly outside of the class but it can access inside the child class.

 



 

Questions asked in Interview on Oops Access Modifiers:

  1. What is an Access modifier in PHP?
  2. What Is a Protected Access modifier?
  3. Can we use Public, Private, and protected as before Class?

 



 

Tags: ,