In what ways can object-oriented programming (OOP) principles be applied to optimize PHP scripts for calendar functionalities?

To optimize PHP scripts for calendar functionalities using object-oriented programming principles, we can create classes for calendar events, months, and years. This allows for better organization of code, reusability, and easier maintenance. By encapsulating calendar functionalities within classes, we can achieve a more structured and efficient approach to managing calendars in PHP.

<?php

// Define a class for calendar events
class CalendarEvent {
    public $title;
    public $date;

    public function __construct($title, $date) {
        $this->title = $title;
        $this->date = $date;
    }

    public function displayEvent() {
        return $this->title . ' on ' . $this->date;
    }
}

// Define a class for months
class Month {
    public $name;
    public $days;

    public function __construct($name, $days) {
        $this->name = $name;
        $this->days = $days;
    }
}

// Define a class for years
class Year {
    public $number;
    public $months = [];

    public function __construct($number) {
        $this->number = $number;
    }

    public function addMonth(Month $month) {
        $this->months[] = $month;
    }
}

// Create instances of classes and use them for calendar functionalities
$event = new CalendarEvent('Meeting', '2022-10-15');
echo $event->displayEvent();

$january = new Month('January', 31);
$february = new Month('February', 28);

$year2022 = new Year(2022);
$year2022->addMonth($january);
$year2022->addMonth($february);

print_r($year2022);

?>