<?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');
- Output for 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.25, 8.4.1 - 8.4.12
- Zaebis! Sohranil: "Vasya"
Perevodim na strnicu oshibki.
Error: Table "user" column "familiya" not found.
preferences:
59.7 ms | 406 KiB | 5 Q