What is the difference between public, private, an

2018-12-31 03:09发布

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() {
  // ...
}

15条回答
公子世无双
2楼-- · 2018-12-31 04:00

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.

  • Abstract eg. Think public visibility scope as "public picnic" that anybody can come to.

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).

  • Think as a protected visibility scope as "Company picnic" where company members and their family members are allowed not the public. It's the most common scope restriction.

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.

  • with picnic analogy think as a "company picnic where only the company members are allowed" in the picnic. not family neither general public are allowed.
查看更多
高级女魔头
3楼-- · 2018-12-31 04:00

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;

查看更多
浮光初槿花落
4楼-- · 2018-12-31 04:01

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:

$myObject = new MyObject()
$myObject->publicVar = 'newvalue';
$pubVar = $myObject->publicVar;

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:

$myObject = new MyObject()
$myObject->setProtectedVar('newvalue');
$protectedVar = $myObject->getProtectedVar();

Private: private properties are very similar to protected properties. But the distinguishing feature/difference is that making it private 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):

$this->setPrivateVar('newvalue');
$privateVar = $this->getPrivateVar();
查看更多
登录 后发表回答