🎓 A1Learner
🔍
0 of 38 lessons completed

PHP Project: Book Library

This project builds a book library, a small catalogue program that lists books and saves the list to a file. The program sorts the books by title, saves them as JSON text, and reads the saved file back.

The Book class holds one book

The constructor stores the title, the author and the price. The line method joins those facts into one line of text.

<?php
class Book {
  public $title;
  public $author;
  public $price;
  public function __construct($title, $author, $price) {
    $this->title = $title;
    $this->author = $author;
    $this->price = $price;
  }
  public function line() {
    return $this->title . " by " . $this->author . " - " . $this->price;
  }
}
$book = new Book("Dune", "Frank Herbert", 12);
echo $book->line() . "\n";

The Library class holds the books

The Library class holds its books in one array. The add method appends one book, and the count method counts the books held.

<?php
class Book {
  public $title;
  public $author;
  public $price;
  public function __construct($title, $author, $price) {
    $this->title = $title;
    $this->author = $author;
    $this->price = $price;
  }
  public function line() {
    return $this->title . " by " . $this->author . " - " . $this->price;
  }
}
class Library {
  public $books = [];
  public function add($book) {
    $this->books[] = $book;
  }
  public function count() {
    return count($this->books);
  }
}
$library = new Library();
$library->add(new Book("Dune", "Frank Herbert", 12));
$library->add(new Book("Emma", "Jane Austen", 7));
echo $library->count() . "\n";
foreach ($library->books as $book) {
  echo $book->line() . "\n";
}

Sorting the books by title

usort orders the books array in place. The comparison function compares two titles with the <=> operator, which reports which title comes first.

<?php
class Book {
  public $title;
  public $author;
  public $price;
  public function __construct($title, $author, $price) {
    $this->title = $title;
    $this->author = $author;
    $this->price = $price;
  }
  public function line() {
    return $this->title . " by " . $this->author . " - " . $this->price;
  }
}
class Library {
  public $books = [];
  public function add($book) {
    $this->books[] = $book;
  }
}
$library = new Library();
$library->add(new Book("Emma", "Jane Austen", 7));
$library->add(new Book("Dune", "Frank Herbert", 12));
$library->add(new Book("Hamlet", "William Shakespeare", 5));
usort($library->books, function ($a, $b) {
  return $a->title <=> $b->title;
});
foreach ($library->books as $book) {
  echo $book->line() . "\n";
}

Saving data as JSON text

json_encode builds JSON text from an array. The program writes that text to a file named library.json and reads the file back into an array.

<?php
$data = [
  ["title" => "Dune", "price" => 12],
  ["title" => "Emma", "price" => 7]
];
$text = json_encode($data);
file_put_contents("library.json", $text);
$saved = json_decode(file_get_contents("library.json"), true);
echo count($saved) . "\n";
echo $saved[1]["title"] . "\n";

Example

<?php
class Book {
  public $title;
  public $author;
  public $price;
  public function __construct($title, $author, $price) {
    $this->title = $title;
    $this->author = $author;
    $this->price = $price;
  }
  public function line() {
    return $this->title . " by " . $this->author . " - " . $this->price;
  }
}
class Library {
  public $books = [];
  public function add($book) {
    $this->books[] = $book;
  }
}
$library = new Library();
$library->add(new Book("Emma", "Jane Austen", 7));
$library->add(new Book("Dune", "Frank Herbert", 12));
$library->add(new Book("Hamlet", "William Shakespeare", 5));
$library->add(new Book("Foundation", "Isaac Asimov", 9));
usort($library->books, function ($a, $b) {
  return $a->title <=> $b->title;
});
foreach ($library->books as $book) {
  echo $book->line() . "\n";
}
$data = [];
foreach ($library->books as $book) {
  $data[] = ["title" => $book->title, "author" => $book->author, "price" => $book->price];
}
file_put_contents("library.json", json_encode($data));
$saved = json_decode(file_get_contents("library.json"), true);
echo "Count: " . count($saved) . "\n";
$total = array_reduce($saved, function ($carry, $item) {
  return $carry + $item["price"];
}, 0);
echo "Total: " . $total . "\n";

Key points

  • A Book object holds the title, the author and the price of one book.
  • The line method of a Book builds one printable line from its facts.
  • A Library object holds its books in one array.
  • usort orders an array in place with a comparison function.
  • JSON is a text format for data, and json_encode builds JSON text from a value.
  • file_put_contents writes text to a file.
  • file_get_contents reads a whole file back as text.
  • array_reduce folds an array into one value, such as a total price.

In this example, four Book objects are added to one Library object. usort orders the books by title, and a loop prints one line for each book. The books are then turned into plain arrays, encoded as JSON text, and written to library.json. The program reads the file back, decodes the text, prints the count of saved books, and uses array_reduce to print the total price.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

Which function folds the saved book rows into one total price?