From the course: PHP: Design Patterns

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Exploring a use case for the table data gateway pattern

Exploring a use case for the table data gateway pattern - PHP Tutorial

From the course: PHP: Design Patterns

Start my 1-month free trial

Exploring a use case for the table data gateway pattern

- With this example we start off pretty simply. With a situation that is way too common in the PHP world. With our queries embedded directly in the HTML. Hopefully your application doesn't suffer from this but if it does, this is your chance to clean it up . Don't worry, we've all been there. In this particular example, we create a connection to the database here and retrieve all the rows from the user table. Then you output the user names of each user. In the second snippet, we load a particular user and then output their username. Overall it's a pretty simple and probably common bit of code. Where things get interesting is when we update this to Table Data Gateway. First in index.php, you can see that we've removed the database connection code. That's not something we need to do here anymore because of the next lines. Notice that as we go forward there isn't any SQL in this file. Instead, the previous query for all the rows has been replaced with a method called "loadAll" on the…

Contents