Chapter 5. Replace global With Dependency Injection

At this point, all of our classes and functions have been consolidated to a central location, and all related include statements have been removed. We would prefer to start writing tests for our classes, but it is very likely that we have a lot of global variables embedded in them. These can cause a lot of trouble via action at a distance where modifying a global in one place changes its value in another place. The next step, then, is to remove all uses of the global keyword from our classes, and inject the necessary dependencies instead.

Note

What Is Dependency Injection?

Dependency injection means that we push our dependencies into a class from the outside, instead of pulling them into a class ...

Get Modernizing Legacy Applications in PHP now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.