Download the BYJU'S Exam Prep App for free GATE/ESE preparation videos & tests - Download the BYJU'S Exam Prep App for free GATE/ESE preparation videos & tests -

Difference Between public, private, and protected in PHP

All of these are the types of access modifiers, and just like the C++ language, the PHP language also has three of these access modifiers. There is a fundamental difference between public, private, and protected in PHP. They help in defining the overall visibility of any given property, constant, or a method by prefixing their declaration using these keywords.

What is a Public Access Modifier?

We use the public keyword for making the functions and variables accessible from anywhere. It enables a user to access it from either the inside of a class or from anywhere outside of it. In other words, if we declare a class member as public, then we can easily access it from anywhere.

What is a Protected Access Modifier?

We use the protected keyword for making the functions and variables accessible for any particularly given class along with its derived class or child classes. In simple words, when we declare the class members as protected, then we can access them only within a class itself along with the parent classes and by inheriting them.

What is a Private Access Modifier?

We use the private keyword for making functions and variables accessible for only one class. No user can access its private properties from outside the class. In simpler words, if we declare a class member as private, then only the class defining the member gets to access it.

Difference Between public, private, and protected in PHP

Here is a list of the differences present between public, private, and protected in PHP.

Parameters Public Access Modifier Private Access Modifier Protected Access Modifier
Basics We use the public keyword for making the functions and variables accessible from anywhere. We use the protected keyword for making the functions and variables accessible for any particularly given class along with its derived class or child classes. We use the private keyword used for making functions and variables accessible for only one class.
Declaration If we declare a class member as public, then we can easily access it from anywhere. When we declare the class members as protected, then we can access them only within a class itself along with the parent classes and by inheritance. If we declare a class member as private, then only the class defining the member gets to access it.

Keep learning and stay tuned to BYJU’S to get the latest updates on GATE Exam along with GATE Eligibility Criteria, GATE 2024, GATE Admit Card, GATE Application Form, GATE Syllabus, GATE Cutoff, GATE Previous Year Question Paper, and more.

Comments

Leave a Comment

Your Mobile number and Email id will not be published.

*

*