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 03:45

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.

查看更多
美炸的是我
3楼-- · 2018-12-31 03:49

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)

查看更多
明月照影归
4楼-- · 2018-12-31 03:49

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 a protected method is being used usually, so i default to protected nowadays. Maybe not 100% objective "in the trenches" experience, I admit.

查看更多
还给你的自由
5楼-- · 2018-12-31 03:52

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.

查看更多
呛了眼睛熬了心
6楼-- · 2018-12-31 03:56

⚡️ Here is an easy way to remember the scope of public, protected and private.

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.
  • No! Objects cannot access this scope

PRIVATE:

  • private scope: A private variable/function is only visible in the current class where it is being defined.
  • No! Class that extend the current class cannot access this scope.
  • No! Objects cannot access this scope.

Read the Visibility of a method or variable on PHP Manual.

查看更多
人间绝色
7楼-- · 2018-12-31 03:57

They're there to allow for different levels of encapsulation

查看更多
登录 后发表回答