What are the design patterns in PHP

PHP design patterns

February 10, 2020
  1. Home
  2. »
  3. For advanced
  4. »
  5. object oriented programing
  6. »
  7. PHP design patterns

Design Patterns, or in German Design pattern, are proven solution patterns for frequently recurring problems. They provide a reusable template for problem solving. As a programmer, you can fall back on these tried and tested design patterns in many ways and thus face the task of generating good program code. In this short tutorial we can only introduce you to the most important design patterns in PHP. If you want to learn more about the topic, I can recommend the book PHP Design Patterns.

Design patterns are here Not to be confused with web design. Design Pattern is about good code, not beautiful web design. All design patterns here relate to object-oriented programming and help you determine how and which classes you should create for your application.

The most important design patterns for PHP are listed and explained in more detail on the following sub-pages.

Get and Set methods
Access to class variables can be controlled using get and set methods. The Get method returns the value for each class variable and the Set method updates it. In this way, it is possible to centrally control how certain class variables can be accessed.

Model-View-Controller in PHP
The Model-View-Controller (MVC) design pattern is one of the most important in PHP. The application is divided into three layers. The model classes bundle all functions to access the database. The views represent your data in the browser. And the controller implements your application logic.

Template pattern
Using the template pattern, a template is generated for different variants of an algorithm. Individual sub-steps of the algorithm are defined as abstract methods. The child classes then implement the concrete implementations of the algorithm.

Author: Nils Reimers