When and why should I use public
, private
, and protected
functions and variables inside a class? What is the difference between them?<
⚡️ 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.