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() {
// ...
}
Answer
You use:
publicscope to make that property/method available from anywhere, other classes and instances of the object.privatescope when you want your property/method to be visible in its own class only.protectedscope when you want to make your property/method visible in all classes that extend current class including the parent class.
More: (For comprehensive information)
No comments:
Post a Comment