Skip to content

lokeshrangani/product-oop-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Product Management System – PHP OOP Example

Overview

This project is a clean and extensible product management system built using Object-Oriented PHP. It demonstrates professional software design principles such as abstraction, inheritance, encapsulation, and polymorphism, while adhering to PSR-4 standards and Composer-based autoloading.

Designed as a practical example, this project focuses on how to structure and scale PHP applications without frameworks — keeping the architecture lightweight, modular, and easy to extend.

Features

Abstract base class for unified product structure.

  • Type-specific product classes (Book, DVD, Furniture) extending shared logic.
  • Dynamic class loading for different product types (no if/else or switch statements).
  • Encapsulated data management via getters and setters.
  • Single endpoint for all product operations.
  • PSR-4 autoloading using Composer.
  • Pure PHP 7+ implementation - no frameworks, just clean OOP.

Project Structure

/src
  /Models
    Product.php
    Book.php
    DVD.php
    Furniture.php
  /Database
    Database.php
index.php
composer.json
README.md

Technology Stack

  • Language: PHP 7.0+
  • Database: MySQL
  • Architecture: Object-Oriented (no frameworks)
  • Autoloading: Composer (PSR-4)
  • Environment: Independent, lightweight, and framework-free

How It Works

Client sends a POST request to a single endpoint (index.php).

  • The system dynamically maps the type (e.g., Book, DVD, Furniture) to its corresponding class using namespaces and autoloading.
  • The product instance handles its own logic via polymorphism — no conditionals required.
  • The Database class saves data through object properties using getters/setters.
  • The result is a clean, extensible architecture that scales easily as new product types are added.

Database Schema

CREATE DATABASE IF NOT EXISTS products_db;
USE products_db;

CREATE TABLE products (
    id INT AUTO_INCREMENT PRIMARY KEY,
    sku VARCHAR(64) NOT NULL UNIQUE,
    name VARCHAR(255) NOT NULL,
    price DECIMAL(10,2) NOT NULL,
    attribute VARCHAR(255) NOT NULL,
    height DECIMAL(10,2) NULL,
    width DECIMAL(10,2) NULL,
    length DECIMAL(10,2) NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

Installation

git clone https://github.com/lokeshrangani/product-oop-example.git
cd product-oop-example
composer dump-autoload

Run the local PHP server:

php -S localhost:8000

Send a POST request (e.g., via Postman or cURL):

POST http://localhost:8000/index.php

Example JSON body:

{
  "type": "book",
  "sku": "BK001",
  "name": "Object-Oriented PHP",
  "price": "19.99",
  "attribute": "1.2"
}

Learning Outcomes

This project is a practical showcase of:

- Clean OOP architecture in PHP
- Polymorphism and abstraction in action
- Eliminating conditional branching with class inheritance
- PSR-4–compliant project structure
- Database encapsulation using object properties

Future Enhancements

- Add data validation and error handling.
- Mass deleting
- Update

License

Open source and free to use for educational or portfolio purposes. You are welcome to clone, modify, and build upon it.

About the Author

An experienced PHP developer with a deep interest in Object-Oriented architecture, scalable backend systems, and clean code principles. Skilled in designing maintainable solutions that follow SOLID and PSR standards, with a focus on practical implementation, performance, and clarity. Believes that good code should be self-explanatory, adaptable, and built to last.

Get in touch:

LinkedIn: linkedin.com/in/lokeshrangani

About

Product Management System (PHP OOP Example)

Topics

Resources

License

Stars

Watchers

Forks

Languages