php简易版图书管理系统
时间: 2023-12-30 18:04:04 浏览: 85
以下是一个用PHP语言编写的简易版图书管理系统的示例:
```php
<?php
// 定义Book类
class Book {
private $title;
private $author;
public function __construct($title, $author) {
$this->title = $title;
$this->author = $author;
}
public function getTitle() {
return $this->title;
}
public function getAuthor() {
return $this->author;
}
}
// 创建图书数组
$books = array(
new Book("Book 1", "Author 1"),
new Book("Book 2", "Author 2"),
new Book("Book 3", "Author 3")
);
// 查询所有图书
function getAllBooks($books) {
foreach ($books as $book) {
echo "Title: " . $book->getTitle() . ", Author: " . $book->getAuthor() . "<br>";
}
}
// 添加图书
function addBook($books, $title, $author) {
$newBook = new Book($title, $author);
$books[] = $newBook;
echo "Book added successfully!";
}
// 修改图书
function updateBook($books, $index, $title, $author) {
if (isset($books[$index])) {
$books[$index] = new Book($title, $author);
echo "Book updated successfully!";
} else {
echo "Invalid book index!";
}
}
// 调用函数演示图书管理系统功能
getAllBooks($books);
addBook($books, "Book 4", "Author 4");
updateBook($books, 1, "Updated Book 2", "Updated Author 2");
```
阅读全文