@Authors Gemini 2.5 flash & XMSI
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.
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:
-
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).
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.
-
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 more0..1
: Zero or one1..*
: One or moren
: Exactly n
Example: A
User
can have anOrder
.<?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 withOrder
. EachOrder
has exactly oneUser
(customer). OneUser
can have zero or moreOrder
s (because a customer can place many orders or none at all).
-
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 ofEmployee
s. 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) containsEmployee
s (the parts). The diamond on theDepartment
side indicates aggregation.1..*
shows that a department can have one or more employees.
-
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 ofOrderItem
s. 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) ownsOrderItem
s (the parts). The filled diamond on theOrder
side indicates composition. EachOrder
has1..*
OrderItem
s.
-
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
andMotorcycle
areVehicle
s.<?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
andMotorcycle
inherit fromVehicle
. The arrow points to the parent class.
-
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
andSMSSender
classes implement theNotifier
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
andSMSSender
implement theNotifier
interface.
- Dependency
Simple Dependency Injection
class Mechanic {
public funciton repair(Tool tool) {
// tool is only used within this method
}
}
- 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.