以下是一个简单的PHP OAO(Object-Action-Object)模式的实例,该模式主要用于描述对象之间的交互过程。
实例描述
在这个实例中,我们有一个学生对象(Student)和一个课程对象(Course)。学生对象可以报名课程,而课程对象可以记录报名的学生。
类定义
我们定义两个类:Student和Course。
```php
class Student {
private $name;
private $courses = [];
public function __construct($name) {
$this->name = $name;
}
public function enrollCourse(Course $course) {
$this->courses[] = $course;
$course->addStudent($this);
}
public function getCourses() {
return $this->courses;
}
}
class Course {
private $name;
private $students = [];
public function __construct($name) {
$this->name = $name;
}
public function addStudent(Student $student) {
$this->students[] = $student;
}
public function getStudents() {
return $this->students;
}
}
```
实例代码
接下来,我们创建一个学生对象和一个课程对象,并展示它们之间的交互。
```php
// 创建学生对象
$student1 = new Student("