<?php
// 定义学生类
class Student {
public $name;
public $id;
public $major;
public function __construct($name, $id, $major) {
$this->name = $name;
$this->id = $id;
$this->major = $major;
}
public function getStudentInfo() {
return "Name: " . $this->name . ", ID: " . $this->id . ", Major: " . $this->major;
}
}
// 创建一个学生对象
$student = new Student("张三", "123456789", "计算机科学与技术");
// 打印学生信息
echo $student->getStudentInfo();
// 定义一个在线服务平台类
class OnlineServicePlatform {
private $students = [];
public function addStudent(Student $student) {
$this->students[] = $student;
}
public function getStudentInfoById($id) {
foreach ($this->students as $student) {
if ($student->id == $id) {
return $student->getStudentInfo();
}
}
return "Student not found.";
}
}
// 创建一个在线服务平台实例
$platform = new OnlineServicePlatform();
// 添加学生到平台
$platform->addStudent($student);
// 通过ID获取学生信息
echo $platform->getStudentInfoById("123456789");
?>