以下是一个使用PHP实现类结构的实例,我们将通过一个简单的图书管理系统来展示如何创建类、构造函数、成员变量和方法。
| 类名 | 成员变量 | 构造函数 | 方法 |
|---|---|---|---|
| Book | title,author,year | __construct($title,$author,$year) | __toString() |
| Library | books | __construct() | getBooks() |
1. Book 类
Book 类用于表示一本图书,包含标题(title)、作者(author)和出版年份(year)。

```php
class Book {
private $title;
private $author;
private $year;
public function __construct($title, $author, $year) {
$this->title = $title;
$this->author = $author;
$this->year = $year;
}
public function __toString() {
return "







