以下是一个简单的PHP对象导向编程(OOP)实例,通过一个类来表示一个学生,包括学生的基本信息和成绩。

实例:学生信息管理系统

类定义

```php

实例php oos,PHP对象导向编程(OOP)实例详解  第1张

class Student {

// 属性

public $name;

public $age;

public $grades = [];

// 构造函数

public function __construct($name, $age) {

$this->name = $name;

$this->age = $age;

}

// 方法:添加成绩

public function addGrade($subject, $score) {

$this->grades[$subject] = $score;

}

// 方法:获取平均成绩

public function getAverage() {

$total = 0;

$count = count($this->grades);

foreach ($this->grades as $score) {

$total += $score;

}

return $count > 0 ? $total / $count : 0;

}

}

```

使用类

```php

// 创建学生对象

$student = new Student("