<?php
	require_once("initPDO.php");
	require_once("createUserTable.php");

	class User
	{
		protected $props;

		public function __get($prop) {
			return $this->props[$prop];
		}

		public function __set($prop, $val) {
			$this->props[$prop] = $val;
		}

		public static function executeRequest($req) {
			global $pdo;
			$request = $pdo->prepare($req);
			if (!$request) {
				myDump(debug_backtrace());
				die('Error while doing request ' . $sqlRequest);
			}

			$request->setFetchMode(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, get_called_class());
			$request->execute();

			return $request->fetchAll();
		}

		public static function getAllUsers() {
			return static::executeRequest('select * from `users`');
		}

		// instance-side method to render a User object to HTML
		public function toHtml() {
			// print_r($this);
			echo "<tr>"
				. "<td>". $this->id . "</td>"
				. "<td>". $this->name . "</td>"
				. "<td>" . $this->email . "</td></tr>";
		}

		// class-side method to render an array of users as an HTML table
		public static function showUsersAsTable($users) {
			echo '<table><thead>
					<tr><th>Id</th><th>Nom</th><th>Email</th></tr></thead><tbody>';
			foreach($users as $u) {
				print_r($u);
				$u->toHtml();
			}
			echo '</tbody></table>';
		}

		public static function showAllUsersAsTable() {
			static::showUsersAsTable(static::getAllUsers());
		}
	}
?>

<!doctype html>
 <html lang="fr">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
	<style>
	table {
		border-top: 1px solid black;
		border-bottom: 1px solid black;
	}

	td {
		text-align: center;
		padding-left: 2em;
		padding-right: 2em;
	}
	</style>

</head>
<body>

<h1>Users</h1>
<?php
	User::showAllUsersAsTable();
?>

</body>
</html>