In PHP, the keywords protected and private are visibility modifiers used to define the accessibility of class properties and methods. These modifiers help control how and where class members (properties or methods) can be accessed.
1. protected
A protected property or method can only be accessed within the class itself and by classes that extend the parent class (subclasses).
This allows inherited classes to use or override the properties or methods, but prevents access from outside the class hierarchy.
php
class ParentClass {
protected $name;
protected function setName($name) {
$this->name = $name;
}
protected function getName() {
return $this->name;
}
}
class ChildClass extends ParentClass {
public function setAndGetName($name) {
$this->setName($name); // Accessing protected method from parent class
return $this->getName(); // Accessing protected method from parent class
}
}
$obj = new ChildClass();
// $obj->setName("John"); // ERROR: Cannot access protected method directly
echo $obj->setAndGetName("John"); // Output: John
2. private
A private property or method can only be accessed within the class in which it is defined.
Even subclasses that extend the parent class do not have access to private properties or methods of the parent class.
php
class ParentClass {
private $name;
private function setName($name) {
$this->name = $name;
}
private function getName() {
return $this->name;
}
public function setAndGetName($name) {
$this->setName($name); // Accessing private method within the same class
return $this->getName(); // Accessing private method within the same class
}
}
class ChildClass extends ParentClass {
public function accessPrivateMethod() {
// $this->setName("John"); // ERROR: Cannot access private method of parent class
}
}
$obj = new ParentClass();
// $obj->setName("John"); // ERROR: Cannot access private method directly
echo $obj->setAndGetName("John"); // Output: John
Key Differences
Visibility Modifier
Access Within Class
Access in Subclass
Access from Outside Class
protected
✅ Yes
✅ Yes
❌ No
private
✅ Yes
❌ No
❌ No
Choosing Between protected and private
Use private when a property or method is specific to the current class and should not be exposed to subclasses.
Use protected when you want to allow subclasses to inherit and potentially override the behavior but still restrict direct access from outside the class hierarchy.
If neither of these fits your needs, you can use the public modifier to make a property or method accessible from anywhere. However, be cautious about overusing public, as it reduces encapsulation.
这种修饰符非常适合希望某些功能能够被子类继承和使用,但不希望暴露给外部代码的场景。
示例代码:
php
class 父类 {
protected $名字;
protected function 设置名字($名字) {
$this->名字 = $名字;
}
protected function 获取名字() {
return $this->名字;
}
}
class 子类 extends 父类 {
public function 设置并获取名字($名字) {
$this->设置名字($名字); // 子类中可以访问父类的 protected 方法
return $this->获取名字(); // 子类中可以访问父类的 protected 方法
}
}
$对象 = new 子类();
// $对象->设置名字("张三"); // 错误:外部无法直接访问 protected 方法
echo $对象->设置并获取名字("张三"); // 输出:张三
2. private (私有的)
使用 private 修饰的属性或方法只能在定义它的类内部访问。
即使子类继承了父类,也无法访问父类的 private 属性或方法。
适合用在希望某些数据或功能严格只对当前类可见的场景。
示例代码:
php
class 父类 {
private $名字;
private function 设置名字($名字) {
$this->名字 = $名字;
}
private function 获取名字() {
return $this->名字;
}
// 提供一个公有方法,让外部间接访问 private 方法
public function 设置并获取名字($名字) {
$this->设置名字($名字); // 在当前类中可以访问 private 方法
return $this->获取名字(); // 在当前类中可以访问 private 方法
}
}
class 子类 extends 父类 {
public function 访问私有方法() {
// $this->设置名字("张三"); // 错误:无法访问父类的 private 方法
}
}
$对象 = new 父类();
// $对象->设置名字("张三"); // 错误:外部无法直接访问 private 方法
echo $对象->设置并获取名字("张三"); // 输出:张三