Menu
Coddy logo textTech

Book and User Classes

Part of the Object Oriented Programming section of Coddy's PHP journey — lesson 83 of 91.

challenge icon

Challenge

Easy

Let's build the foundation of our Library Management System by creating the Book and User classes. These core domain objects will work together to manage library operations.

You'll organize your code across three files:

  • Book.php — Create a Book class that tracks library books. Your book should have three readonly properties set via constructor promotion: isbn, title, and author. Include a private $isAvailable property that starts as true. Add three methods:
    • isAvailable(): bool — returns the availability status
    • markAsBorrowed(): void — sets availability to false
    • markAsReturned(): void — sets availability to true
  • User.php — Include the Book file and create a User class representing library members. Your user should have two readonly properties via constructor promotion: id (int) and name (string). Include a private array $borrowedBooks (initially empty) and a constant MAX_BOOKS set to 3. Add these methods:
    • getBorrowedBooks(): array — returns the borrowed books array
    • canBorrow(): bool — returns true if the user has fewer than MAX_BOOKS borrowed
    • addBook(Book $book): void — adds a book to the borrowed array
    • removeBook(string $isbn): void — removes a book by ISBN using array_filter
  • main.php — Include the User file and bring your classes to life. You'll receive four inputs: a user ID, user name, book ISBN, and book title (author will be "Unknown").

    Create a User and a Book with the provided values. Print whether the user can borrow initially, then have the user borrow the book (mark it as borrowed and add it to the user). Finally, print the book's availability and the count of borrowed books.

    Output format:

    Can borrow: [yes/no]
    Book available: [yes/no]
    Books borrowed: [count]

Notice how encapsulation protects the internal state — a book's availability only changes through dedicated methods, and a user's borrowed books are managed through controlled access. This foundation will support the borrowing system we'll build next.

Try it yourself

<?php

require_once 'User.php';

// Read inputs
$userId = intval(trim(fgets(STDIN)));
$userName = trim(fgets(STDIN));
$bookIsbn = trim(fgets(STDIN));
$bookTitle = trim(fgets(STDIN));
$bookAuthor = "Unknown";

// TODO: Create a User object with the provided id and name

// TODO: Create a Book object with the provided isbn, title, and author

// TODO: Print whether the user can borrow initially
// Format: "Can borrow: yes" or "Can borrow: no"

// TODO: Have the user borrow the book:
// - Mark the book as borrowed
// - Add the book to the user's borrowed books

// TODO: Print the book's availability
// Format: "Book available: yes" or "Book available: no"

// TODO: Print the count of borrowed books
// Format: "Books borrowed: [count]"

?>

All lessons in Object Oriented Programming