Skip to content

Instantly share code, notes, and snippets.

@MuhammadQuran17
Last active June 2, 2025 14:05
Show Gist options
  • Save MuhammadQuran17/04c06bc39530227eab019c7ff85f7879 to your computer and use it in GitHub Desktop.
Save MuhammadQuran17/04c06bc39530227eab019c7ff85f7879 to your computer and use it in GitHub Desktop.
UML diagrams

@Authors Gemini 2.5 flash & XMSI

Understanding the Basics of UML Diagrams with PHP Examples

UML (Unified Modeling Language) is a graphical language used for visualizing, specifying, constructing, and documenting software system artifacts. It helps developers and stakeholders better understand a system's structure and behavior.

Let's break down the fundamentals of UML diagrams, focusing on class diagrams and the relationships between classes, using PHP examples.

Core Concepts of UML Class Diagrams

Within the context of classes, the most frequently used UML diagram is the class diagram. It illustrates the structure of a system, composed of classes, their attributes, operations (methods), and the relationships between them.

Key elements on a class diagram:

  1. Class: Represented by a rectangle divided into three sections:

    • Class Name: In the top section.
    • Attributes: In the middle section. These represent the properties of the class.
    • Operations/Methods: In the bottom section. These represent the behavior of the class.

    Example PHP Class and its UML Representation:

    <?php
    class User {
        private string $name;
        private string $email;
        public int $age;
    
        public function __construct(string $name, string $email) {
            $this->name = $name;
            $this->email = $email;
        }
    
        public function getName(): string {
            return $this->name;
        }
    
        public function getEmail(): string {
            return $this->email;
        }
    
        public function setAge(int $age): void {
            $this->age = $age;
        }
    }
    ?>

    UML Representation of the User class:

    +-----------------+
    |      User       |
    +-----------------+
    | - name: string  |
    | - email: string |
    | + age: int      |
    +-----------------+
    | + __construct(name: string, email: string) |
    | + getName(): string |
    | + getEmail(): string |
    | + setAge(age: int): void |
    +-----------------+
    
    • Visibility:
      • + (public): Public (accessible from anywhere).
      • - (private): Private (accessible only within the class).
      • # (protected): Protected (accessible within the class and its child classes).

Relationships Between Classes

image source: https://www.umlboard.com/docs/relations/

Relationships show how classes interact with each other. This is a crucial aspect of understanding a system's architecture.

  1. Association:

    • Represents a general connection between two classes. They can interact with each other.
    • Indicated by a solid line between the classes.
    • Can have multiplicity, which specifies the number of instances of one class related to instances of another.
      • 1: Exactly one
      • *: Zero or more
      • 0..1: Zero or one
      • 1..*: One or more
      • n: Exactly n

    Example: A User can have an Order.

    <?php
    class User {
        // ... (User attributes and methods) ...
    }
    
    class Order {
        private User $customer;
        // ... other Order attributes and methods ...
    
        public function __construct(User $customer) {
            $this->customer = $customer;
        }
    }
    ?>

    UML Representation of Association:

    +-----------------+        +-----------------+
    |      User       |        |      Order      |
    +-----------------+        +-----------------+
    |                 | 1    * |                 |
    |                 |--------| customer        |
    |                 |        |                 |
    +-----------------+        +-----------------+
    
    • Here, User is associated with Order. Each Order has exactly one User (customer). One User can have zero or more Orders (because a customer can place many orders or none at all).
  2. Aggregation:

    • A specific type of association representing a "part-of" relationship, but where the parts can exist independently of the whole.
    • Indicated by a solid line with an unfilled diamond on the "whole" side.

    Example: A Department consists of Employees. An employee can exist and work in another department even if the current department is disbanded.

    <?php
    class Employee {
        private string $name;
    
        public function __construct(string $name) {
            $this->name = $name;
        }
    
        public function getName(): string {
            return $this->name;
        }
    }
    
    class Department {
        private string $name;
        private array $employees = [];
    
        public function __construct(string $name) {
            $this->name = $name;
        }
    
        public function addEmployee(Employee $employee): void {
            $this->employees[] = $employee;
        }
    
        public function getEmployees(): array {
            return $this->employees;
        }
    }
    ?>

    UML Representation of Aggregation:

    +-----------------+         +-----------------+
    |   Department    |         |    Employee     |
    +-----------------+    1..* +-----------------+
    | - name: string  |<>-------| - name: string  |
    |                 |         |                 |
    +-----------------+         +-----------------+
    
    • Department (the whole) contains Employees (the parts). The diamond on the Department side indicates aggregation. 1..* shows that a department can have one or more employees.
  3. Composition:

    • A stronger "part-of" relationship than aggregation. The parts cannot exist without the whole. If the whole is destroyed, the parts are also destroyed.
    • Indicated by a solid line with a filled diamond on the "whole" side.

    Example: An Order consists of OrderItems. If an order is deleted, its order items are also deleted.

    <?php
    class OrderItem {
        private string $productName;
        private int $quantity;
    
        public function __construct(string $productName, int $quantity) {
            $this->productName = $productName;
        }
    
        public function getDetails(): string {
            return $this->productName . " x " . $this->quantity;
        }
    }
    
    class Order {
        private array $items = [];
    
        public function addItem(OrderItem $item): void {
            $this->items[] = $item;
        }
    
        public function getTotalItems(): int {
            return count($this->items);
        }
    }
    ?>

    UML Representation of Composition:

    +-----------------+               +-----------------+
    |      Order      |               |    OrderItem    |
    +-----------------+ 1        1..* +-----------------+
    |                 |<black>--------| - productName:str|
    |                 |               | - quantity: int  |
    +-----------------+               +------------------+
    
    • Order (the whole) owns OrderItems (the parts). The filled diamond on the Order side indicates composition. Each Order has 1..* OrderItems.
  4. Generalization / Inheritance:

    • Represents an "is-a" relationship. A child class inherits attributes and methods from a parent class.
    • Indicated by a solid line with an unfilled triangular arrow pointing to the parent class.

    Example: Car and Motorcycle are Vehicles.

    <?php
    class Vehicle {
        protected string $make;
        protected string $model;
    
        public function __construct(string $make, string $model) {
            $this->make = $make;
            $this->model = $model;
        }
    
        public function getInfo(): string {
            return "Make: " . $this->make . ", Model: " . $this->model;
        }
    }
    
    class Car extends Vehicle {
        private int $numDoors;
    
        public function __construct(string $make, string $model, int $numDoors) {
            parent::__construct($make, $model);
            $this->numDoors = $numDoors;
        }
    
        public function getInfo(): string {
            return parent::getInfo() . ", Doors: " . $this->numDoors;
        }
    }
    
    class Motorcycle extends Vehicle {
        private bool $hasSidecar;
    
        public function __construct(string $make, string $model, bool $hasSidecar) {
            parent::__construct($make, $model);
            $this->hasSidecar = $hasSidecar;
        }
    
        public function getInfo(): string {
            return parent::getInfo() . ", Has Sidecar: " . ($this->hasSidecar ? "Yes" : "No");
        }
    }
    ?>
    • Car and Motorcycle inherit from Vehicle. The arrow points to the parent class.
  5. Realization / Interface:

    • Shows that a class implements methods defined in an interface.
    • Indicated by a dashed line with an unfilled triangular arrow pointing to the interface.

    Example: EmailSender and SMSSender classes implement the Notifier interface.

    <?php
    interface Notifier {
        public function send(string $message): void;
    }
    
    class EmailSender implements Notifier {
        public function send(string $message): void {
            echo "Sending email: " . $message . "\n";
        }
    }
    
    class SMSSender implements Notifier {
        public function send(string $message): void {
            echo "Sending SMS: " . $message . "\n";
        }
    }
    ?>

    UML Representation of Realization:

    +-----------------+               +-----------------+
    |   <<interface>> |               |  EmailSender    |
    |     Notifier    |<-- . . . . .  +-----------------+
    +-----------------+               |                 |
    | + send(message: string): void | | + send(message: string): void |
    +-----------------+               +-----------------+
             ^
             | . . . . .
             |
    +-----------------+
    |   SMSSender     |
    +-----------------+
    |                 |
    | + send(message: string): void |
    +-----------------+
    
    • The dashed line with an arrow indicates that EmailSender and SMSSender implement the Notifier interface.

  1. Dependency

Simple Dependency Injection

class Mechanic {
  public funciton repair(Tool tool) {
    // tool is only used within this method
  }
}

Why Use UML Diagrams?

  • Visualization: They help in better understanding complex system structures.
  • Communication: They facilitate clear communication among developers, architects, and stakeholders.
  • Documentation: They serve as excellent documentation for future system support and enhancements.
  • Design: They help identify design issues and shortcomings early in the development process.

UML is a powerful tool for software modeling. By starting with class diagrams and understanding the relationships between them, you lay a strong foundation for designing well-structured and maintainable PHP applications.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment