When and why should I use public
, private
, and protected
functions and variables inside a class?
(什么时候以及为什么应该在类内使用public
, private
和protected
函数和变量?)
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() {
// ...
}
ask by Adam translate from so 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…