3v4l.org

run code in 200+ PHP versions simultaneously
<?php namespace Cookpedia; class Entry { private $hydrated = false; private $data = [ 'id' => 0, 'title' => null, 'latin_title' => null, 'category' => 0, 'calories' => 0, 'proteins' => 0, 'fats' => 0, 'carbohydrates' => 0, 'description' => null, 'tags' => null, 'measures' => [], ]; public function hydrate(PDO $dbh, int $id): void { $stmt = $dbh->prepare("SELECT `title`, `latin_title`, `category`, `calories`, `proteins`, `fats`, `carbohydrates`, `description`, `measures`, `tags` FROM `cook_encyclopedia_entries` WHERE `id`=:id"); $result = $stmt->execute($id); $this->data = $result->fetch(PDO::FETCH_ASSOC); $this->data['measures'] = $this->computeMeasures($this->data['measures']); $this->hydrated = true; } public function getData(): array { if (!$this->hydrated) { throw new \Exception('hydrate first'); } return $this->data; } private function computeMeasures(string $measures): array { $measureArray = explode('|', $measures); return [ 'g_in_tsp' => $measureArray[0], 'g_in_tbsp' => $measureArray[1], 'g_in_glass' => $measureArray[2], 'g_in_cup' => $measureArray[3], ]; } }
based on nHP59
Output for 7.1.25 - 7.3.5