Repository in PHP -- Design pattern with examples

Click for: original source

The Repository pattern is used to create a middle layer between a data layer (which can be a database, a file, a CSV, etc) and your controllers. It is mainly used on large scale application, where there will be dozens of reference to a single data layer. It gives the ability to change the data layer without affecting any code in your controller. By Anastasio Nico.

The repository pattern probably is best known for its widespread use within MVC application. In a normal application the flow works like this: The users reach a view or an endpoint requesting some data, this request need to be processed by a controller that in the majority of the cases will communicate with a source of data. Usually, that would be a database.

Repository pattern may be considered structural because of its similarity with the Facade pattern but at the same time, it is kinda creational because with some easy addition can look like a Factory.

For the example of the code in PHP and further explanation how it can be implemented follow the link to the original article. Nice one!

[Read More]

Tags php web-development programming open-source