3v4l.org

run code in 500+ PHP versions simultaneously
<?php class Producto { protected $codigo; protected $nombre; protected $precio; public function __construct($codigo, $nombre, $precio) { $this->codigo = $codigo; $this->nombre = $nombre; $this->precio = $precio; } public function getPrecio(): float { return $this->precio; } public function etiqueta(): string { return $this->codigo . " · " . htmlspecialchars($this->nombre) . " · " . number_format($this->precio, 2, ',', '.') . " €"; } } class LineaPedido { private $producto; private $unidades; public function __construct($producto, $unidades) { $this->producto = $producto; $this->unidades = $unidades; } public function importe(): float { return $this->producto->getPrecio() * $this->unidades; } public function texto(): string { return $this->producto->etiqueta() . " × " . $this->unidades . " = " . number_format($this->importe(), 2, ',', '.') . " €"; } } interface Cobrable { public function total(): float; } class Pedido implements Cobrable { private $lineas = []; public function anadir(LineaPedido $linea): void { $this->lineas[] = $linea; } public function total(): float { $suma = 0.0; foreach ($this->lineas as $linea) { $suma += $linea->importe(); } return $suma; } public function ticket(): string { $texto = "Ticket · Cafetería IES Balmaseda BHI <br>"; foreach ($this->lineas as $linea) { $texto .= $linea->texto() . "<br>"; } $texto .= 'Total: ' . number_format($this->total(), 2, ',', '.') . ' €'; return $texto; } } $cafe = new Producto('B', 'Café con leche', 1.50); $bocata = new Producto('C', 'Bocadillo', 2.80); $pedido = new Pedido(); $pedido->anadir(new LineaPedido($cafe, 2)); $pedido->anadir(new LineaPedido($bocata, 1)); ?> <!DOCTYPE html> <html lang="es"> <head> <meta charset="UTF-8"> <title>TPV</title> </head> <body> <pre><?php echo $pedido->ticket(); ?></pre> </body> </html>
Output for 8.2.0 - 8.2.35, 8.3.0 - 8.3.36, 8.4.1 - 8.4.27, 8.5.0 - 8.5.12
<!DOCTYPE html> <html lang="es"> <head> <meta charset="UTF-8"> <title>TPV</title> </head> <body> <pre>Ticket · Cafetería IES Balmaseda BHI <br>B · Café con leche · 1,50 € × 2 = 3,00 €<br>C · Bocadillo · 2,80 € × 1 = 2,80 €<br>Total: 5,80 €</pre> </body> </html>

preferences:
16.71 ms | 1160 KiB | 6 Q