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() {
// ...
}
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.
You use:
public
scope to make that variable/function available from anywhere, other classes and instances of the object.private
scope when you want your variable/function to be visible in its own class only.protected
scope when you want to make your variable/function visible in all classes that extend current class including the parent class.More: (For comprehensive information)
Considering 'when':
I tend to declare everything as private initially, if I'm not exactly sure. Reason being, that it's usually much easier to turn a private method public than the other way round. That's because you can at least be sure that the private method hasn't been used anywhere but in the class itself. A public method may already be in use everywhere, possibly requiring an extensive re-write.
Update: i go for a default of
protected
nowadays, because I've come to find that it's good enough for encapsulation and doesn't get in the way when I'm extending classes (which i try to avoid anyway). Only if i have a good reason to use the other two, i will.A good reason for a
private
method would be one that implements something inherent to that object that even an extending class should not change (factual reason, in addition to encapsulation, like internal state management). Eventually, it's still easy enough to track down where aprotected
method is being used usually, so i default toprotected
nowadays. Maybe not 100% objective "in the trenches" experience, I admit.The difference is as follows:
Public
:: A public variable or method can be accessed directly by any user of the class.Protected
:: A protected variable or method cannot be accessed by users of the class but can be accessed inside a subclass that inherits from the class.Private
:: A private variable or method can only be accessed internally from the class in which it is defined.This means that a private variable or method cannot be called from a child that extends the class.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.
They're there to allow for different levels of encapsulation