How can the use of classes in PHP be optimized for handling different delivery times in a shop system?
To optimize the use of classes in PHP for handling different delivery times in a shop system, we can create a DeliveryTime class with properties for the delivery time and a method to calculate the estimated delivery date based on the current date and the specified delivery time. This class can be used to easily manage and display different delivery options in the shop system.
class DeliveryTime {
private $deliveryTime;
public function __construct($deliveryTime) {
$this->deliveryTime = $deliveryTime;
}
public function getEstimatedDeliveryDate() {
$currentDate = new DateTime();
$estimatedDeliveryDate = $currentDate->modify('+' . $this->deliveryTime . ' days')->format('Y-m-d');
return $estimatedDeliveryDate;
}
}
// Example usage
$standardDelivery = new DeliveryTime(3);
$expressDelivery = new DeliveryTime(1);
echo 'Standard Delivery: ' . $standardDelivery->getEstimatedDeliveryDate() . '<br>';
echo 'Express Delivery: ' . $expressDelivery->getEstimatedDeliveryDate();