Admin Interface
Part of the Object Oriented Programming section of Coddy's PHP journey — lesson 86 of 91.
Challenge
EasyLet's extend our Library Management System by adding administrative capabilities. You'll create an Admin class that inherits from User but has special powers to manage the library's inventory — adding new books and removing existing ones.
You'll organize your code across five files:
Book.php— YourBookclass with readonly properties (isbn,title,author), a private$isAvailableproperty starting astrue, and methods:isAvailable(),markAsBorrowed(), andmarkAsReturned().User.php— Include the Book file. YourUserclass with readonly properties (id,name), a private$borrowedBooksarray, aMAX_BOOKSconstant of3, and methods:getBorrowedBooks(),canBorrow(),addBook(Book $book), andremoveBook(string $isbn).Library.php— Include the User file. YourLibraryclass with the existing functionality includingaddBook(),findBook(),borrowBook(),returnBook(), and the search methods. Add a new method:removeBook(string $isbn): void— removes a book from the collection usingunset()
Admin.php— Include the Library file. Create anAdminclass that extendsUser. Admins inherit all user capabilities but gain two administrative methods:addBookToLibrary(Library $library, Book $book): string— adds the book to the library and returns"Added: [title]"removeBookFromLibrary(Library $library, string $isbn): string— attempts to remove a book with these checks:- If the book doesn't exist, return
"Book not found" - If the book is currently borrowed, return
"Cannot remove: book is currently borrowed" - Otherwise, remove it and return
"Removed: [title]"
- If the book doesn't exist, return
main.php— Include the Admin file. You'll receive two inputs: an ISBN and a book title.Create a
Libraryand anAdminwith ID1and name"AdminUser". Also create a regularUserwith ID2and name"RegularUser".Create a
Bookwith the provided ISBN, title, and author"Test Author". Perform these operations and print each result on a new line:- Have the admin add the book to the library
- Have the regular user borrow the book
- Have the admin try to remove the book (should fail — it's borrowed)
- Have the regular user return the book
- Have the admin remove the book (should succeed now)
- Have the admin try to remove the same book again (should fail — not found)
This challenge demonstrates how inheritance creates a natural hierarchy — admins are users with extra privileges. The safety checks in removeBookFromLibrary ensure data integrity by preventing removal of books that are still in circulation.
Try it yourself
<?php
require_once 'Admin.php';
// Read input
$isbn = trim(fgets(STDIN));
$title = trim(fgets(STDIN));
// TODO: Create a Library
// TODO: Create an Admin with ID 1 and name "AdminUser"
// TODO: Create a regular User with ID 2 and name "RegularUser"
// TODO: Create a Book with the provided ISBN, title, and author "Test Author"
// TODO: Perform these operations and print each result on a new line:
// 1. Have the admin add the book to the library
// 2. Have the regular user borrow the book
// 3. Have the admin try to remove the book (should fail - it's borrowed)
// 4. Have the regular user return the book
// 5. Have the admin remove the book (should succeed now)
// 6. Have the admin try to remove the same book again (should fail - not found)
?>All lessons in Object Oriented Programming
1Fundamentals of OOP
External FilesIntroduction to OOPClasses vs ObjectsThe $this KeywordMethodsPropertiesConstructor (__construct)Destructor (__destruct)Recap - Simple Calculator4Inheritance
Basic InheritanceThe parent:: KeywordMethod OverridingThe final KeywordAbstract ClassesRecap - Employee Hierarchy7Encapsulation
Public, Protected, PrivateAccess Modifiers in DepthGetters and SettersInformation HidingConstructor Promotion (8.0)Recap - Student Records System2Namespaces & Autoloading
Introduction to NamespacesThe use KeywordPSR-4 Autoloading StandardComposer AutoloaderRecap - Organized Project5Interfaces & Contracts
Introduction to InterfacesImplementing InterfacesMultiple Interface ImplementInterface vs Abstract ClassType Hinting with InterfacesRecap - Shape Calculator8Magic Methods
Magic Methods Introduction__toString & __debugInfo__get, __set, __isset, __unset__call & __callStatic__clone & Object Cloning__serialize & __unserializeRecap - Custom Collection11Type System & Error Handling
Type DeclarationsNullable TypesUnion & Intersection TypesException ClassesCustom Exception HierarchyTry, Catch, FinallyRecap - Form Validator14Project: Library Management
Project OverviewBook and User Classes3Class Properties
Instance vs Static PropertiesConstants in ClassesStatic Methods & PropertiesPrivate & Protected PropertiesReadonly Properties (PHP 8.1)Recap - Bank Account Manager6Polymorphism
Method Overriding RevisitedPolymorphism via InterfacesType Hinting & Union TypesLate Static BindingRecap - Payment Processor9Traits
Introduction to TraitsUsing Multiple TraitsTrait Conflict ResolutionAbstract Methods in TraitsTraits vs Inheritance12Design Patterns Part 1
Intro to Design PatternsSingleton PatternFactory PatternObserver PatternStrategy Pattern