Consider this PHP code using a simple repository pattern to fetch user data.
<?php
interface UserRepository {
public function findUserById(int $id): ?array;
}
class InMemoryUserRepository implements UserRepository {
private array $users = [
1 => ['id' => 1, 'name' => 'Alice'],
2 => ['id' => 2, 'name' => 'Bob']
];
public function findUserById(int $id): ?array {
return $this->users[$id] ?? null;
}
}
$repo = new InMemoryUserRepository();
$user = $repo->findUserById(2);
echo $user['name'] ?? 'Not found';
?>What will this code print?
<?php interface UserRepository { public function findUserById(int $id): ?array; } class InMemoryUserRepository implements UserRepository { private array $users = [ 1 => ['id' => 1, 'name' => 'Alice'], 2 => ['id' => 2, 'name' => 'Bob'] ]; public function findUserById(int $id): ?array { return $this->users[$id] ?? null; } } $repo = new InMemoryUserRepository(); $user = $repo->findUserById(2); echo $user['name'] ?? 'Not found'; ?>
Look at the user ID requested and the data stored in the repository.
The repository stores two users: Alice with ID 1 and Bob with ID 2. The code fetches user with ID 2, which is Bob, so it prints 'Bob'.
Choose the best description of the Repository pattern in software design.
Think about how the Repository pattern helps organize code related to data.
The Repository pattern abstracts data access, providing a simple interface to retrieve and store domain objects, keeping business logic separate from data storage details.
Examine this PHP code snippet using a repository pattern:
<?php
class UserRepository {
private array $users = [];
public function findUserById(int $id): array {
return $this->users[$id];
}
}
$repo = new UserRepository();
$user = $repo->findUserById(1);
echo $user['name'];
?>What error will this code cause when run?
<?php class UserRepository { private array $users = []; public function findUserById(int $id): array { return $this->users[$id]; } } $repo = new UserRepository(); $user = $repo->findUserById(1); echo $user['name']; ?>
Check what data is stored in the repository before accessing it.
The users array is empty, so accessing $this->users[1] causes an undefined array key error.
Given this interface:
<?php
interface ProductRepository {
public function findProductById(int $id): ?array;
}
?>Which class correctly implements this interface?
Check method signature matches exactly including return type and parameter types.
Option C matches the interface method signature exactly, including parameter type and nullable array return type.
Consider this PHP repository class and usage:
<?php
class ItemRepository {
private array $items = [];
public function addItem(array $item): void {
$this->items[$item['id']] = $item;
}
public function removeItem(int $id): void {
unset($this->items[$id]);
}
public function getAllItems(): array {
return array_values($this->items);
}
}
$repo = new ItemRepository();
$repo->addItem(['id' => 1, 'name' => 'Pen']);
$repo->addItem(['id' => 2, 'name' => 'Pencil']);
$repo->addItem(['id' => 3, 'name' => 'Eraser']);
$repo->removeItem(2);
$repo->addItem(['id' => 2, 'name' => 'Marker']);
$items = $repo->getAllItems();
echo count($items);
?>What number will be printed?
<?php class ItemRepository { private array $items = []; public function addItem(array $item): void { $this->items[$item['id']] = $item; } public function removeItem(int $id): void { unset($this->items[$id]); } public function getAllItems(): array { return array_values($this->items); } } $repo = new ItemRepository(); $repo->addItem(['id' => 1, 'name' => 'Pen']); $repo->addItem(['id' => 2, 'name' => 'Pencil']); $repo->addItem(['id' => 3, 'name' => 'Eraser']); $repo->removeItem(2); $repo->addItem(['id' => 2, 'name' => 'Marker']); $items = $repo->getAllItems(); echo count($items); ?>
Count how many unique items remain after adding and removing by ID.
Initially 3 items are added. Item with ID 2 is removed, then a new item with ID 2 is added again. So total items are 3.