When and why should I use public
, private
, and protected
functions and variables inside a class? What is the difference between them?
Examples:
// Public
public $variable;
public function doSomething() {
// ...
}
// Private
private $variable;
private function doSomething() {
// ...
}
// Protected
protected $variable;
protected function doSomething() {
// ...
}
Visibility Scopes with Abstract Examples :: Makes easy Understanding
This visibility of a property or method is defined by pre-fixing declaration of one of three keyword (Public, protected and private)
Public : If a property or method is defined as public, it means it can be both access and manipulated by anything that can refer to object.
Protected : when a property or method visibility is set to protected members can only be access within the class itself and by inherited & inheriting classes. (Inherited:- a class can have all the properties and methods of another class).
Private : When a property or method visibility is set to private, only the class that has the private members can access those methods and properties(Internally within the class), despite of whatever class relation there maybe.
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 classprotected $familyWealth;
For me, this is the most useful way to understand the three property types:
Public: Use this when you are OK with this variable being directly accessed and changed from anywhere in your code.
Example usage from outside of the class:
Protected: Use this when you want to force other programmers (and yourself) to use getters/setters outside of the class when accessing and setting variables (but you should be consistent and use the getters and setters inside the class as well). This or
private
tend to be the default way you should set up all class properties.Why? Because if you decide at some point in the future (maybe even in like 5 minutes) that you want to manipulate the value that is returned for that property or do something with it before getting/setting, you can do that without refactoring everywhere you have used it in your project.
Example usage from outside of the class:
Private:
private
properties are very similar toprotected
properties. But the distinguishing feature/difference is that making itprivate
also makes it inaccessible to child classes without using the parent class's getter or setter.So basically, if you are using getters and setters for a property (or if it is used only internally by the parent class and it isn't meant to be accessible anywhere else) you might as well make it
private
, just to prevent anyone from trying to use it directly and introducing bugs.Example usage inside a child class (that extends MyObject):