0
0
PHPprogramming~10 mins

Repository pattern in PHP - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare the Repository interface.

PHP
<?php
interface [1] {
    public function findAll();
}
Drag options to blanks, or click blank then click option'
AController
BDatabase
CService
DRepository
Attempts:
3 left
💡 Hint
Common Mistakes
Using unrelated names like Service or Controller for the interface.
2fill in blank
medium

Complete the method signature to find an item by its ID.

PHP
<?php
interface Repository {
    public function findById([1] $id);
}
Drag options to blanks, or click blank then click option'
Astring
Bint
Cbool
Darray
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect types like bool or array for the ID parameter.
3fill in blank
hard

Fix the error in the repository implementation class declaration.

PHP
<?php
class UserRepository [1] Repository {
    public function findAll() {
        // code to get all users
    }
}
Drag options to blanks, or click blank then click option'
Auses
Bextends
Cimplements
Dinherits
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'extends' keyword for interfaces causes errors.
4fill in blank
hard

Fill both blanks to complete the method that saves a user entity.

PHP
<?php
class UserRepository implements Repository {
    public function save([1] $user) : [2] {
        // code to save user
    }
}
Drag options to blanks, or click blank then click option'
AUser
Bbool
Cvoid
Darray
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong parameter types or return types like void or array.
5fill in blank
hard

Fill all three blanks to complete the repository method that deletes a user by ID.

PHP
<?php
class UserRepository implements Repository {
    public function delete([1] $id) : [2] {
        // code to delete user
        return [3];
    }
}
Drag options to blanks, or click blank then click option'
Aint
Bbool
Ctrue
Dfalse
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong types or returning false by default.