When and why should I use public
, private
, and protected
functions and variables inside a class? What is the difference between them?<
When we follow object oriented php in our project , we should follow some rules to use access modifiers in php. Today we are going to learn clearly what is access modifier and how can we use it.PHP access modifiers are used to set access rights with PHP classes and their members that are the functions and variables defined within the class scope. In php there are three scopes for class members.
Now, let us have a look at the following image to understand access modifier access level
Now, let us have a look at the following list to know about the possible PHP keywords used as access modifiers.
public :- class or its members defined with this access modifier will be publicly accessible from anywhere, even from outside the scope of the class.
private :- class members with this keyword will be accessed within the class itself. we can’t access private data from subclass. It protects members from outside class access.
protected :- same as private, except by allowing subclasses to access protected superclass members.
Now see the table to understand access modifier Read full article php access modifire