The replacement process
The next step, then, is to remove all use of new
keyword from our non-Factory classes, and inject the necessary dependencies instead. We will also use Factory objects as needed to deal with repeated creation dependencies. This is the general process we will follow:
- Find a class with the
new
keyword in it. If the class is already aFactory
, we can ignore it and move on. - For each one-time creation in the class:
- Extract each instantiation to a constructor parameter.
- Assign the constructor parameter to a property.
- Remove any constructor parameters and class properties that are used only for the
new
call.
- For each repeated creation in the class:
- Extract each block of creation code to a new
Factory
class. - Create a constructor parameter ...
- Extract each block of creation code to a new
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.