Database design: keys, relationships and normalization
Design tables that avoid duplicated data: primary keys, foreign keys, one-to-many and many-to-many relationships.
Catch every error in one place, log it, and show a safe page to visitors.
<?php
// bootstrap.php
const DEBUG = false;
set_error_handler(function (int $no, string $msg, string $file, int $line): bool {
if (!(error_reporting() & $no)) {
return false; // respect the @ operator
}
throw new ErrorException($msg, 0, $no, $file, $line);
});
set_exception_handler(function (Throwable $e): void {
$line = date('c') . ' ' . get_class($e) . ': ' . $e->getMessage()
. ' in ' . $e->getFile() . ':' . $e->getLine() . "\n";
error_log($line, 3, __DIR__ . '/logs/app.log');
http_response_code(500);
echo DEBUG ? '<pre>' . htmlspecialchars((string)$e) . '</pre>' : 'Something went wrong. Please try again later.';
});
register_shutdown_function(function (): void {
$err = error_get_last();
if ($err && in_array($err['type'], [E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR], true)) {
error_log(date('c') . ' FATAL ' . $err['message'] . ' ' . $err['file'] . ':' . $err['line'] . "\n", 3, __DIR__ . '/logs/app.log');
}
});logs folder private and rotate old logs.Design tables that avoid duplicated data: primary keys, foreign keys, one-to-many and many-to-many relationships.
A folder layout and a few habits that keep a growing PHP site tidy, secure and easy to maintain.