What are some best practices for structuring PHP code to handle multiple events and their associated images in a gallery format?

When structuring PHP code to handle multiple events and their associated images in a gallery format, it's best to use an object-oriented approach to keep the code organized and maintainable. One way to achieve this is by creating classes for events and images, with relationships between them. Using a database to store event and image data can also help in efficiently managing and retrieving the information.

class Event {
    private $eventId;
    private $eventName;
    private $eventDate;
    private $images = [];

    public function __construct($eventId, $eventName, $eventDate) {
        $this->eventId = $eventId;
        $this->eventName = $eventName;
        $this->eventDate = $eventDate;
    }

    public function addImage($image) {
        $this->images[] = $image;
    }

    public function getImages() {
        return $this->images;
    }
}

class Image {
    private $imageId;
    private $imageUrl;

    public function __construct($imageId, $imageUrl) {
        $this->imageId = $imageId;
        $this->imageUrl = $imageUrl;
    }

    public function getImageUrl() {
        return $this->imageUrl;
    }
}

// Usage example
$event = new Event(1, 'Event Name', '2022-01-01');
$event->addImage(new Image(1, 'image1.jpg'));
$event->addImage(new Image(2, 'image2.jpg'));

$images = $event->getImages();
foreach ($images as $image) {
    echo '<img src="' . $image->getImageUrl() . '" alt="Event Image">';
}