0
0
PHPprogramming~20 mins

Repository pattern in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Repository Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Repository pattern example?

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
<?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';
?>
ANot found
BBob
CAlice
DFatal error: Uncaught Error: Undefined array key 2
Attempts:
2 left
💡 Hint

Look at the user ID requested and the data stored in the repository.

🧠 Conceptual
intermediate
1:30remaining
Which statement best describes the Repository pattern?

Choose the best description of the Repository pattern in software design.

AIt separates data access logic from business logic by providing a collection-like interface to access domain objects.
BIt is a pattern to directly connect the UI with the database for faster queries.
CIt is a design pattern that forces all data to be stored in memory for quick access.
DIt is a pattern that replaces the need for any database or external storage.
Attempts:
2 left
💡 Hint

Think about how the Repository pattern helps organize code related to data.

🔧 Debug
advanced
2:00remaining
What error does this Repository pattern code produce?

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
<?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'];
?>
ANo error, prints empty string
BNotice: Undefined variable: users
CParse error: syntax error, unexpected 'echo' (T_ECHO)
DFatal error: Uncaught Error: Undefined array key 1
Attempts:
2 left
💡 Hint

Check what data is stored in the repository before accessing it.

📝 Syntax
advanced
2:00remaining
Which option correctly implements a Repository interface in PHP?

Given this interface:

<?php
interface ProductRepository {
    public function findProductById(int $id): ?array;
}
?>

Which class correctly implements this interface?

A
class MyRepo implements ProductRepository {
    public function findProductById(int $id): array {
        return [];
    }
}
B
class MyRepo implements ProductRepository {
    public function findProductById($id) {
        return [];
    }
}
C
class MyRepo implements ProductRepository {
    public function findProductById(int $id): ?array {
        return null;
    }
}
D
class MyRepo implements ProductRepository {
    public function findProductById(int $id) {
        return null;
    }
}
Attempts:
2 left
💡 Hint

Check method signature matches exactly including return type and parameter types.

🚀 Application
expert
2:30remaining
How many items are in the repository after these operations?

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
<?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);
?>
A3
B2
C1
D4
Attempts:
2 left
💡 Hint

Count how many unique items remain after adding and removing by ID.