@ 2024-02-09T06:11:00Z <?php
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 . '"';
}
}
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');
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
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"
Fatal error: Uncaught Exception: Table "user" column "familiya" not found. in /in/fugVD:17
Stack trace:
#0 /in/fugVD(39): Schema::getTableColumnByName('user', 'familiya')
#1 {main}
thrown in /in/fugVD on line 17
Process exited with code 255 . preferences:dark mode live preview ace vim emacs key bindings
63.1 ms | 406 KiB | 5 Q