Book description
Get your code under control in a series of small, specific steps
About This Book
- Learn to extract and replace legacy artifacts,
- Improve your application from the ground up while keeping your codebase fully operational,
- Improve the quality of your legacy applications.
Who This Book Is For
PHP developers from all skill levels will be able to get value from this book and will be able to transform their spaghetti code applications to clean, modular applications. 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 inherited, this is the book for you. All you need is to have PHP 5.0 installed, and you’re all set to change the way you maintain and deploy your code!
What You Will Learn
- Replace global and new with dependency injection
- Extract SQL statements to gateways
- Convert action logic to controllers
- Remove repeated logic in page scripts
- Create maintainable PHP code from crufty legacy PHP
In Detail
Have you noticed that your legacy PHP application is composed of page scripts placed directly in the document root of the web server? Or, 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? Working with such a legacy application feels like dragging your feet through mud, doesn’t it?This book will show you how to modernize your application in terms of practice and technique, rather than in terms of using tools like frameworks and libraries, 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. We’ll show you how dependency injection can replace both the new and global dependencies. We’ll also show you how to change the presentation logic to view files and the action logic to a controller. Moreover, we’ll 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. Most of the very limited code we will 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.
Style and approach
This book gives developers an easy-to-follow, practical and powerful process to bring their applications up to a modern baseline. Each step in the book is practical, self-contained and moves you closer to the end goal you seek: maintainable code. As you follow the exercises in the book, the author almost anticipates your questions and you will have the answers, ready to be implemented on your project.
Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the code file.
Table of contents
-
Modernizing Legacy Applications in PHP
- Table of Contents
- Modernizing Legacy Applications in PHP
- Credits
- Foreword
- About the Author
- Acknowledgement
- www.PacktPub.com
- Preface
- 1. Legacy Applications
- 2. Prerequisites
- 3. Implement an Autoloader
-
4. Consolidate Classes and Functions
- Consolidate Class Files
- Consolidate functions into class files
-
Common Questions
- Should we remove the autoloader include call?
- How should we pick files for candidate include calls?
- What if an include defines more than one class?
- What if the one-class-per-file rule is disagreeable?
- What if a Class or Function is defined inline?
- What if a definition file also executes logic?
- What about third-party libraries?
- For functions, can we use instance methods instead of static methods?
- Review and next steps
- 5. Replace global With Dependency Injection
- 6. Replace new with Dependency Injection
-
7. Write Tests
- Fighting test resistance
-
The way of Testivus
- Setting up a test suite
-
Common Questions
- Can we skip this step and do it later?
- Come On, Really, Can We Do This Later?
- What about hard-to-test classes?
- What about our earlier characterization tests?
- Should we test private and protected methods?
- Can we change a test after we write it?
- Do we need to test Third-party libraries?
- What about code coverage?
- Review and next steps
-
8. Extract SQL statements to Gateways
-
Embedded SQL Statements
- The extraction process
- Search for SQL statements
- An initial Gateway class method
-
Common Questions
- What about INSERT, UPDATE, and DELETE Statements?
- What about Repetitive SQL strings?
- What about complex query strings?
- What about queries inside non-Gateway classes?
- Can we extend from a base Gateway class?
- What about multiple queries and complex result structures?
- What if there is no Database Class?
- Review and next steps
-
Embedded SQL Statements
- 9. Extract Domain Logic to Transactions
-
10. Extract Presentation Logic to View Files
- Embedded presentation logic
- Review and next steps
-
11. Extract Action Logic to Controllers
- Embedded action logic
- Review and next steps
-
12. Replace Includes in Classes
- Embedded include Calls
-
The Replacement process
- Search for include Calls
- Replacing a Single include Call
- Replacing Multiple include Calls
- Copy include file to Class Method
- Replace the original include Call
- Discover coupled variables through testing
- Replace other include Calls and Test
- Delete the include file and test
- Common QuestionsCan one class receive logic from many include files?
- What about include calls originating in non-class files?
- Review and next steps
- 13. Separate Public and Non-Public Resources
- 14. Decouple URL Paths from File Paths
- 15. Remove Repeated Logic in Page Scripts
- 16. Add a Dependency Injection Container
- 17. Conclusion
- A. Typical Legacy Page Script
- B. Code before Gateways
- C. Code after Gateways
- D. Code after Transaction Scripts
- E. Code before Collecting Presentation Logic
- F. Code after Collecting Presentation Logic
- G. Code after Response View File
- H. Code after Controller Rearrangement
- I. Code after Controller Extraction
- J. Code after Controller Dependency Injection
- Index
Product information
- Title: Modernizing Legacy Applications in PHP
- Author(s):
- Release date: August 2016
- Publisher(s): Packt Publishing
- ISBN: 9781787124707
You might also like
book
PHP 5 Unleashed
Over 12 million Internet domains worldwide use the PHP language to power their websites. If you …
book
Web Application Development with PHP 4.0
As Web sites and intranets become larger and more complex, static HTML files hit their limits. …
book
PHP 7: Real World Application Development
Use new features of PHP 7 to solve practical, real-world problems faced by PHP developers like …
book
Pro PHP Programming
If you are a web programmer, you need to know modern PHP. This book presents with …