Build a Mini Library Management System in PHP (OOP + Composer Autoloading)

If you’re learning Object-Oriented PHP, a great way to practice is by building something small and meaningful.

In this guide, we’ll build a Mini Library Management System that will help you understand:

✅ OOP concepts
✅ Namespaces
✅ Dependency Injection
✅ Composer Autoloading

Let’s go step by step.

🎯 Goal:

Create a small PHP app to manage books (add, list, and search) using OOP principles, namespaces, and Composer autoloading.


🧩 Project Overview

We’ll build a simple CLI-based app that can:

  • Add books to a library
  • List all books
  • Search a book by title

All using OOP principles and Composer autoloading.


🧱 Step 1: Project Structure

Create a folder named library-app and inside it, we’ll have this structure:

library-app/
├── composer.json
├── src/
│   ├── Models/
│   │   └── Book.php
│   ├── Services/
│   │   └── Library.php
│   └── index.php
└── vendor/

⚙️ Step 2: Initialize Composer

Run this in your terminal:

composer init

After setup, add the PSR-4 autoload configuration:

{
  "autoload": {
    "psr-4": {
      "App\\": "src/"
    }
  }
}

Then run:

composer dump-autoload

Now Composer will automatically load any class under the App\\ namespace.


📘 Step 3: Create the Book Class

📄 src/Models/Book.php

<?php
namespace App\Models;

class Book
{
    private string $title;
    private string $author;
    private int $year;

    public function __construct(string $title, string $author, int $year)
    {
        $this->title  = $title;
        $this->author = $author;
        $this->year   = $year;
    }

    public function getTitle(): string
    {
        return $this->title;
    }

    public function getAuthor(): string
    {
        return $this->author;
    }

    public function getYear(): int
    {
        return $this->year;
    }

    public function getDetails(): string
    {
        return "{$this->title} by {$this->author} ({$this->year})";
    }
}

Concepts Used

  • Encapsulation → private properties
  • Getters → controlled data access
  • Constructor → initialize object on creation

📚 Step 4: Create the Library Class

📄 src/Services/Library.php

<?php
namespace App\Services;

use App\Models\Book;

class Library
{
    private array $books = [];

    public function addBook(Book $book): void
    {
        $this->books[] = $book;
    }

    public function listBooks(): void
    {
        foreach ($this->books as $book) {
            echo $book->getDetails() . PHP_EOL;
        }
    }

    public function findBookByTitle(string $title): ?Book
    {
        foreach ($this->books as $book) {
            if (strtolower($book->getTitle()) === strtolower($title)) {
                return $book;
            }
        }
        return null;
    }
}

Concepts Used

  • Dependency InjectionBook object passed into Library
  • Encapsulation → private $books property
  • Clean separation of logic (Single Responsibility Principle)

🖥️ Step 5: Create index.php

📄 src/index.php

<?php
require __DIR__ . '/../vendor/autoload.php';

use App\Services\Library;
use App\Models\Book;

$library = new Library();

$library->addBook(new Book('Clean Code', 'Robert C. Martin', 2008));
$library->addBook(new Book('The Pragmatic Programmer', 'Andrew Hunt', 1999));

echo "\n📚 List of Books:\n";
$library->listBooks();

$searchTitle = 'Clean Code';
echo "\n\n🔍 Searching for: $searchTitle\n";
$found = $library->findBookByTitle($searchTitle);
if ($found) {
    echo "Found: " . $found->getDetails();
} else {
    echo "Book not found!";
}

▶️ Step 6: Run the Project

In your terminal, run:

php src/index.php

You should see:

📚 List of Books:
Clean Code by Robert C. Martin (2008)
The Pragmatic Programmer by Andrew Hunt (1999)

🔍 Searching for: Clean Code
Found: Clean Code by Robert C. Martin (2008)

🧠 Concepts You Practiced

ConceptExplanation
EncapsulationPrivate properties with getters/setters
Inheritance & PolymorphismExtend Book later as Ebook with its own getDetails()
AbstractionCould create abstract Publication class as blueprint
Dependency InjectionLibrary depends on Book, passed as an object
NamespaceApp\\Models, App\\Services
AutoloadingComposer PSR-4 autoloading
PSR-12Clean and modern coding style

🏁 Final Thoughts

This mini project may look simple, but it gives you a strong foundation in OOP, namespaces, dependency injection, and autoloading — exactly what modern frameworks like Laravel use behind the scenes.

If you can build this confidently, you’re ready to move on to MVC architecture and eventually Laravel framework internals.