Modernizing Legacy Applications In PHP
Modernizing Legacy Applications In PHP
Get your code under control in a series of small, specific steps.
About the Book
"You will breeze through your code like the wind. It will be autoloaded, dependency-injected, unit-tested, layer-separated, and front-controlled."
Is your legacy PHP application composed of page scripts placed directly in the document root of the web server? Do your page scripts, along with any other classes and functions, combine the concerns of model, view, and controller into the same scope? Is the majority of the logical flow incorporated as include files and global functions rather than class methods? If so, you already know that the wide use of global variables means that making a change in one place leads to unexpected consequences somewhere else. These and other factors make it overly difficult and expensive for you to add features and fix bugs. Working with this legacy application feels like dragging your feet through mud.
But it doesn't have to be that way! This book will show you how to modernize your application by extracting and replacing its legacy artifacts. We will use a step-by-step approach, moving slowly and methodically, to improve your application from the ground up. Moreover, we will keep your application running the whole time. Each completed step in the process will keep your codebase fully operational with higher quality. When we are done, you will be able to breeze through your code like the wind. Your code will be autoloaded, dependency-injected, unit-tested, layer-separated, and front-controlled.
Please note that this book is about modernizing in terms of practice and technique, and not in terms of tools. We are not going to discuss the latest, hottest frameworks or libraries. With the exception of testing systems like PHPUnit, and one or two standalone third-party libraries, the book does not advocate adding third-party code to your existing legacy application. Most of the very limited code we do add to your application is specific to this book. We will be improving ourselves as programmers, as well as improving the quality of our legacy application.
If you feel overwhelmed by a legacy application, "Modernizing Legacy Applications in PHP" is the book for you. If you prefer a paper copy, you can purchase one at Lulu.com.
If you're still on the fence, the video "It Was Like That When I Got Here" (embedded above on this page) outlines the first few chapters for free, and you can read some reviews of the book on the feedback page. You can also read the 4.5-star review from SitePoint.
Be sure to follow the book on Twitter @mlaphp, and tell all your friends!
Bundles that include this book
Reader Testimonials
jblotus
Easy-to-follow, practical, and powerful
[This book] gives developers an easy-to-follow, practical and powerful process to bring their applications up to a modern baseline. As I followed the exercises in the book, my questions almost seemed to be anticipated and answered before the chapter was over. If you are in the midst of a legacy refactor or you find yourself in a state of despair caused by the code you have to work on, I implore you: read Modernizing Legacy Applications in PHP. Change is really possible!
Chris Smith
Kiss your technical debt goodbye
This book helped me slay a 300k line of code giant and has allowed me to break out my shell. It has been my best PHP book purchase yet and I continue to improve the code base. The code base is now 80% unit tested and I am now implementing continuous integration.
jclermont
A 12-step program for PHP devs
Reading through the book, it feels like you're pair programming with the author. I'm at the keyboard, driving, and the author is navigating, telling me where to go and what to do next. Each step is practical, self-contained and moves you closer to the end goal you seek: maintainable code. I *highly* recommend this book. Even if you're a seasoned developer (I've been writing code professionally more than 20 years), you will benefit from Paul's approach and detailed documentation of the process.
J. Michael Ward
Superb
This is one of those books that PHP developers from all skill levels will be able to glean value from, and I know after just a single read-through that it will be an oft-referenced resource when I need to convert my old legacy-based procedural code into something cleaner, object-oriented, and testable. This is a very thorough guide to ... getting developers stuck with legacy codebases up to speed with the tools that are available to them. I will recommend this to anyone who will listen.
Table of Contents
- Foreword
- Preface and Acknowledgments
-
1. Legacy Applications
- The Typical PHP Application
- Rewrite or Refactor?
- Legacy Frameworks
- Review and Next Steps
-
2. Prerequisites
- Revision Control
- PHP Version
- Editor/IDE
- Style Guide
- Test Suite
- Review and Next Steps
-
3. Implement An Autoloader
- PSR-0
- A Single Location For Classes
- Add Autoloader Code
- Common Questions
- Review and Next Steps
-
4. Consolidate Classes and Functions
- Consolidate Class Files
- Consolidate Functions Into Class Files
- Common Questions
- Review and Next Steps
-
5. Replace
global
With Dependency Injection- Global Dependencies
- The Replacement Process
- Common Questions
- Review and Next Steps
-
6. Replace
new
With Dependency Injection- Embedded Instantiation
- The Replacement Process
- Common Questions
- Review and Next Steps
-
7. Write Tests
- Fighting Test Resistance
- Setting Up A Test Suite
- Common Questions
- Review and Next Steps
-
8. Extract SQL Statements To Gateways
- Embedded SQL Statements
- The Extraction Process
- Common Questions
- Review and Next Steps
-
9. Extract Domain Logic To Transactions
- Embedded Domain Logic
- Domain Logic Patterns
- The Extraction Process
- Common Questions
- Review and Next Steps
-
10. Extract Presentation Logic To View Files
- Embedded Presentation Logic
- The Extraction Process
- Common Questions
- Review and Next Steps
-
11. Extract Action Logic To Controllers
- Embedded Action Logic
- The Extraction Process
- Common Questions
- Review and Next Steps
-
12. Replace Includes In Classes
-
Embedded
include
Calls - The Replacement Process
- Common Questions
- Review and Next Steps
-
Embedded
-
13. Separate Public And Non-Public Resources
- Intermingled Resources
- The Separation Process
- Common Questions
- Review and Next Steps
-
14. Decouple URL Paths From File Paths
- Coupled Paths
- The Decoupling Process
- Common Questions
- Review and Next Steps
-
15. Remove Repeated Logic In Page Scripts
- Repeated Logic
- The Removal Provess
- Common Questions
- Review and Next Steps
-
16. Add A Dependency Injection Container
- What Is A Dependency Injection Container?
- Adding A DI Container
- Common Questions
- Review and Next Steps
-
17. Conclusion
- Opportunities for Improvement
- Conversion to Framework
- Review and Next Steps
- Appendix A: Typical Legacy Page Script
- Appendix B: Code Before Gateways
- Appendix C: Code After Gateways
- Appendix D: Code After Transaction Scripts
- Appendix E: Code Before Collecting Presentation Logic
- Appendix F: Code After Collecting Presentation Logic
- Appendix G: Code After Response View File
- Appendix H: Code After Controller Rearrangement
- Appendix I: Code After Controller Extraction
- Appendix J: Code After Controller Dependency Injection
- Colophon
- About the Author
The Leanpub 60-day 100% Happiness Guarantee
Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.
See full terms
Do Well. Do Good.
Authors have earned$11,812,115writing, publishing and selling on Leanpub, earning 80% royalties while saving up to 25 million pounds of CO2 and up to 46,000 trees.
Learn more about writing on Leanpub
Free Updates. DRM Free.
If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).
Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.
Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.
Learn more about Leanpub's ebook formats and where to read them
Top Books
Stratospheric
Tom Hombergs, Björn Wilmsmann, and Philip RiecksFrom Zero to Production with Spring Boot and AWS. All you need to know to get a Spring Boot application into production with AWS. No previous AWS knowledge required.
Go to stratospheric.dev for a tour of the contents.
C++20 - The Complete Guide
Nicolai M. JosuttisAll new language and library features of C++20 (for those who know previous C++ versions).
The book presents all new language and library features of C++20. Learn how this impacts day-to-day programming, to benefit in practice, to combine new features, and to avoid all new traps.
Buy early, pay less, free updates.
Other books:
OpenIntro Statistics
David Diez, Christopher Barr, Mine Cetinkaya-Rundel, and OpenIntroA complete foundation for Statistics, also serving as a foundation for Data Science.
Leanpub revenue supports OpenIntro (US-based nonprofit) so we can provide free desk copies to teachers interested in using OpenIntro Statistics in the classroom and expand the project to support free textbooks in other subjects.
More resources: openintro.org.
Jetpack Compose internals
Jorge CastilloJetpack Compose is the future of Android UI. Master how it works internally and become a more efficient developer with it. You'll also find it valuable if you are not an Android dev. This book provides all the details to understand how the Compose compiler & runtime work, and how to create a client library using them.
Advanced Web Application Architecture
Matthias NobackThe missing manual for making your web applications future-proof
Ansible for DevOps
Jeff GeerlingAnsible is a simple, but powerful, server and configuration management tool. Learn to use Ansible effectively, whether you manage one server—or thousands.
Maîtriser Apache JMeter
Philippe Mouawad, Bruno Demion (Milamber), and Antonio Gomes RodriguesToute la puissance d'Apache JMeter expliquée par ses commiteurs et utilisateurs experts. De l'intégration continue en passant par le Cloud, vous découvrirez comment intégrer JMeter à vos processus "Agile" et Devops.
If you're looking for the newer english version of this book, go to Master JMeter : From load testing to DevOps
Aprendiendo Git
Miguel Angel Durán GarcíaGit no es complicado... ¡Si lo entiendes! 😜
¿Sientes que sabes usarlo porque has memorizado todos los comandos que necesitas? ¡Pero no entiendes qué hace cada cosa y por qué! Así es normal que, cuando exista un problema, te cueste resolverlo.
¡Con este libro vas a entender de una vez por todas todo lo que es Git y cómo sacarle provecho!
D3 Start to Finish
Peter CookD3 Start to Finish shows you how to build a custom, interactive and beautiful data visualisation using the JavaScript library D3.js (versions 6 & 7).
The book covers D3.js concepts such as selections, joins, requests, scale functions, events & transitions. You'll put these concepts into practice by building a custom, interactive data visualisation.
R Programming for Data Science
Roger D. PengThis book brings the fundamentals of R programming to you, using the same material developed as part of the industry-leading Johns Hopkins Data Science Specialization. The skills taught in this book will lay the foundation for you to begin your journey learning data science. Printed copies of this book are available through Lulu.
Top Bundles
- #1
Software Architecture
2 Books
"Software Architecture for Developers" is a practical and pragmatic guide to modern, lightweight software architecture, specifically aimed at developers. You'll learn:The essence of software architecture.Why the software architecture role should include coding, coaching and collaboration.The things that you really need to think about before... - #2
CCIE Service Provider Ultimate Study Bundle
2 Books
Piotr Jablonski, Lukasz Bromirski, and Nick Russo have joined forces to deliver the only CCIE Service Provider training resource you'll ever need. This bundle contains a detailed and challenging collection of workbook labs, plus an extensively detailed technical reference guide. All of us have earned the CCIE Service Provider certification... - #4
Pattern-Oriented Memory Forensics and Malware Detection
2 Books
This training bundle for security engineers and researchers, malware and memory forensics analysts includes two accelerated training courses for Windows memory dump analysis using WinDbg. It is also useful for technical support and escalation engineers who analyze memory dumps from complex software environments and need to check for possible... - #6
Retromat eBook Bundle for Agile Retrospectives
2 Books
If you facilitate retrospectives this bundle is for you: "Plans for Retrospectives" helps beginners learn the lay of the land with tried-and-true plans. Once you know your way around, "Run great agile retrospectives" contains all 135+ activities in Retromat for you to mix and match. - #7
Static Analysis and Automated Refactoring
2 Books
As PHP developers we are living in the "Age of Static Analysis". We can use a tool like PHPStan to learn about potential bugs before we ship our code to production, and we can enforce our team's programming standards using custom PHPStan rules. Recipes for Decoupling by Matthias Noback teaches you in great detail how to do this, while also... - #9
Practical FP in Scala + Functional event-driven architecture
2 Books
Practical FP in Scala (A hands-on approach) & Functional event-driven architecture, aka FEDA, (Powered by Scala 3), together as a bundle! The content of PFP in Scala is a requirement to understand FEDA so why not take advantage of this bundle!?