0
0
PHPprogramming~10 mins

Repository pattern in PHP - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Repository pattern
Client calls Repository
Repository handles data logic
Repository calls Data Source (DB, API)
Data Source returns data
Repository returns data to Client
The client asks the repository for data. The repository talks to the data source, gets data, and returns it to the client.
Execution Sample
PHP
<?php
interface UserRepository {
  public function findUserById(int $id): array;
}

class UserRepo implements UserRepository {
  public function findUserById(int $id): array {
    // Simulate DB call
    return ['id' => $id, 'name' => 'Alice'];
  }
}

$userRepo = new UserRepo();
$user = $userRepo->findUserById(1);
print_r($user);
?>
This code shows a repository interface and class that returns user data by ID, simulating a database call.
Execution Table
StepActionMethod CalledInputOutput
1Create UserRepo instance--UserRepo object created
2Call findUserByIdfindUserById1Array ['id' => 1, 'name' => 'Alice']
3Print result--Array printed: id=1, name=Alice
4End--No more actions
💡 All steps completed, user data retrieved and printed.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
$userReponullUserRepo objectUserRepo objectUserRepo objectUserRepo object
$usernullnull['id' => 1, 'name' => 'Alice']['id' => 1, 'name' => 'Alice']['id' => 1, 'name' => 'Alice']
Key Moments - 2 Insights
Why do we use an interface for the repository?
The interface defines what methods the repository must have, so the client code can use any class that follows this interface without changing. See step 2 in execution_table where findUserById is called on the repository.
Does the client know how data is fetched?
No, the client only calls the repository method. The repository hides the data source details. This is shown in step 2 where the repository returns data without exposing database logic.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output of findUserById when called with 1?
ANull
BError message
CArray with id=1 and name='Alice'
DEmpty array
💡 Hint
Check step 2 in execution_table where findUserById returns the array.
At which step is the UserRepo object created?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at step 1 in execution_table where the UserRepo instance is created.
If the repository method returned data from an API instead of a database, what changes in the execution_table?
AThe method called would change
BThe output would still be the same format
CThe client code would change
DThe UserRepo object would not be created
💡 Hint
Repository hides data source, so output format stays same as shown in variable_tracker.
Concept Snapshot
Repository pattern:
- Defines an interface for data access
- Repository class implements data fetching logic
- Client calls repository, not data source directly
- Hides data source details (DB, API)
- Makes code easier to change and test
Full Transcript
The Repository pattern helps separate data access logic from the rest of the code. The client calls methods on a repository interface. The repository class implements these methods and fetches data from a database or other source. This hides the details of data fetching from the client. In the example, a UserRepo class implements a method to find a user by ID and returns user data as an array. The client creates the repository object, calls the method, and gets the data. This makes the code cleaner and easier to maintain.