在PHP编程中,私有(private)和公共(public)关键字用于控制类成员(属性和方法)的访问权限。以下是一个简单的实例,展示如何使用这些关键字。
类定义
```php

class Car {
// 公共属性
public $color;
// 私有属性
private $speed;
// 公共方法
public function __construct($color) {
$this->color = $color;
$this->speed = 0;
}
// 公共方法,设置速度
public function setSpeed($speed) {
$this->speed = $speed;
}
// 私有方法,获取速度
private function getSpeed() {
return $this->speed;
}
// 公共方法,显示信息
public function displayInfo() {
echo "







