<?php
function redirextExceptions(Throwable $exception) {
echo 'Perevodim na strnicu oshibki.' . PHP_EOL.
'Error: ' . $exception->getMessage() . PHP_EOL;
}
set_exception_handler('redirextExceptions');
class Schema {
private static array $tables = [
'user' => [
'id' => 'int',
'name' => 'string',
]
];
public static function getTableColumnByName(string $tableName, string $columnName): TableColumn {
if (false === array_key_exists($tableName, static::$tables)) {
throw new Exception(static::class . ' table "' . $tableName . '" not found.');
}
if (false === array_key_exists($columnName, static::$tables[$tableName])) {
throw new Exception('Table "' . $tableName . '" column "' . $columnName . '" not found.');
}
return new TableColumn($columnName, static::$tables[$tableName][$columnName]);
}
}
class UserTableGateway {
public function update(int $id, TableColumn $column, mixed $value): void {
echo 'Zaebis! Sohranil: "' . $value . '"' . PHP_EOL;
}
}
class TableColumn {
public function __construct(
public readonly string $name,
public readonly string $type,
) {}
}
$gateway = new UsertableGateway;
$gateway->update(33, Schema::getTableColumnByName('user', 'name'), 'Vasya');
$gateway->update(33, Schema::getTableColumnByName('user', 'familiya'), 'Vasya');
preferences:
27.36 ms | 404 KiB | 5 Q