When and why should I use public
, private
, and protected
functions and variables inside a class? What is the difference between them?<
Variables in PHP are cast in three different type:
Public : values of this variable types are available in all scope and call on execution of you code.
declare as: public $examTimeTable;
Private: Values of this type of variable are only available on only to the class it belongs to.
private $classRoomComputers;
Protected: Values of this class only and only available when Access been granted in a form of inheritance or their child class. generally used ::
to grant access by parent class
protected $familyWealth;
You use:
public
scope to make that property/method available from anywhere, other classes and instances of the object.
private
scope when you want your property/method to be visible in its own class only.
protected
scope when you want to make your property/method visible in all classes that extend current class including the parent class.
If you don't use any visibility modifier, the property / method will be public.
More: (For comprehensive information)
private
- can be accessed from WITHIN the class only
protected
- can be accessed from WITHIN the class and INHERITING classes
public
- can be accessed from code OUTSIDE the class as well
This applies to functions as well as variables.
⚡️ Here is an easy way to remember the scope of
public
,protected
andprivate
.
PUBLIC
:
public
scope: A public variable/function is available to both objects and other classes.PROTECTED
:
protected
scope: A protected variable/function is available to all the classes that extend the current class. PRIVATE
:
private
scope: A private variable/function is only visible in the current class where it is being defined.Read the Visibility of a method or variable on PHP Manual.
Public
: is a default state when you declare a variable or method, can be accessed by anything directly to the object.
Protected
: Can be accessed only within the object and subclasses.
Private
: Can be referenced only within the object, not subclasses.
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