Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

How to Tackle the Beast: Upgrade A Legacy Code Base to PHP 7

Hypothetically speaking, what if you wanted to change all of the lightbulbs in your house to use LED bulbs, but your current wiring didn’t support LED bulbs? You would need to re-wire your house, which would take a long time. In the meantime, you wouldn’t have any working electricity. Is there a better way? What if there was an adapter that could transition LED to work with old wiring if necessary, or if the wiring was new, just work as usual? That’s a metaphor for how I figured out how to gradually upgrade a legacy (LAMP) code base to be compliant with PHP 7.0. Since PHP deprecated the MySQL extension functions as of PHP 5.5.0 and removed in 7.0, we needed to transition a LOT of code in a legacy system to use the modern MySQLi extension functions. In trying to figure out how to upgrade the code base without ceasing new development on the system, we decided to use an adapter database class to allow us to transition gradually. In this session, we'll explain how the adapter class works and also the rules we put in place to ensure there was an end to the transition tunnel.

  • Be the first to comment

  • Be the first to like this

How to Tackle the Beast: Upgrade A Legacy Code Base to PHP 7

  1. 1. How to Gradually Upgrade a Legacy Code Base to PHP 7 Ann Gaffigan ZendCon 2017 TACKLING THE BEAST
  2. 2. HOW MANY OF YOU ARE STILL USING <= PHP 5?
  3. 3. HOW MANY OF YOU ARE STILL USING <= PHP 5? Source: w3techs.com
  4. 4. THE BAD NEWS:
  5. 5. THE GOOD NEWS: • You do NOT have to stop all further production until you transition your code to PHP 7 • You do not have to adjust the server’s architecture • There are some tools to help you • You can gradually update your codebase while it still uses PHP 5 to be PHP 7-compatible
  6. 6. THE GRADUAL UPDATE PLAN: • Update your existing code piece by piece to be both PHP 5- AND PHP 7- compatible • This way, it can run on PHP 5 until you’re ready. When you’ve finished updating all of your code, it’s PHP 7-compatible and ready to migrate.
  7. 7. DISADVANTAGES: • It will take longer than if you stopped everything to upgrade your code base. • You’ll have to wait to use any of the newly added features in PHP 7
  8. 8. NEW FEATURES IN PHP 7:
  9. 9. NEW FEATURES IN PHP 7:
  10. 10. NEW FEATURES IN PHP 7: http://php.net/manual/en/migration70.new-features.php
  11. 11. CHANGED FUNCTIONS IN PHP 7: http://php.net/manual/en/migration70.changed-functions.php • debug_zval_dump() • dirname() • getrusage() • mktime() and gmmktime() • preg_replace() function no longer supports "e” • setlocale() • exec(), system() and passthru() shmop_open(), shmop_write(), shmop_read(), shmop_close() and shmop_delete(). • substr() and iconv_substr() • xml_set_object()
  12. 12. BACKWARDS-INCOMPATIBLE CHANGES IN PHP 7: http://php.net/manual/en/migration70.incompatible.php
  13. 13. BACKWARDS-INCOMPATIBLE CHANGES IN PHP 7: http://php.net/manual/en/migration70.incompatible.php
  14. 14. BACKWARDS-INCOMPATIBLE CHANGES IN PHP 7 • Minor changes to foreach with regards to the internal array pointer and handling of the array being iterated over. • Integer handling: • Octal literals • Negative bitshifts • Out of range bitshifts • String handling: • Hexadecimals are no longer considered numeric • Use filter_var($str, FILTER_VALIDATE_INT, FILTER_FLAG_ALLOW_HEX) http://php.net/manual/en/migration70.incompatible.php
  15. 15. REMOVED FUNCTIONS IN PHP 7 • ereg* • split() • mysql_* • (not an exhaustive list) http://php.net/manual/en/migration70.incompatible.php
  16. 16. EREG/PREG • ereg(“abc”, $string) - Yes • preg_match(“abc”, $string) – No • preg_match(“/abc/”, $string) - Yes
  17. 17. SPLIT/PREG_SPLIT • split(“abc”, $string) – Yes • preg_split(“abc”, $string) – No • preg_split(“/abc/”, $string) - Yes
  18. 18. MYSQL_*
  19. 19. SO WE’RE CONNECTED. NOW WHAT? • Some of the mysqli functions are simply the mysql function with “i” added: (This is not an exhaustive list.)
  20. 20. SO WE’RE CONNECTED. NOW WHAT? • Others require additional parameters, namely the connection link. (This is not an exhaustive list.)
  21. 21. TYPICAL CODE IN THE LEGACY CODE BASE require ‘dbconnect.php’; $sql="SELECT thing FROM table WHERE thing IS NOT NULL"; $result=mysql_query($sql); while($row=mysql_fetch_array($result)) { echo $row['thing']. "<br>"; }
  22. 22. UPDATED TO BE PHP 7-COMPATIBLE require ‘dbconnect2.php’; //use $link from here $sql="SELECT thing FROM table WHERE thing IS NOT NULL"; $result=mysqli_query($link, $sql); while($row=mysqli_fetch_array($result)) { echo $row['thing']. "<br>"; }
  23. 23. HOW TO SET UP A TIMELINE • Think about your code base over the course of a year. How much of it do you think you’ll touch in a year? • For us, we felt like we would touch most of the code at some point that year • It may be a longer or shorter timeframe for you • Naturally work your way through the code for 75% of the timeframe • Spend the last 25% prioritizing the compatibility changes so you hit your deadline
  24. 24. 75% OF THE TIMELINE: AU NATURAL • When you’re going to make functional changes to some code: 1. Modernize it FIRST • Analyze what needs to be changed • Change what’s necessary • Test the modernized code 2. Merge the modernized code 3. Make the intended updates to the code functionality 4. Test & Merge the updated code
  25. 25. FINAL 25% OF THE TIMELINE: INTENTIONAL • Use tools to determine what code is still not PHP 7- Compatible • PHP CodeSniffer • There are also tools to make some of the easy changes • BUT: You want to cement PHP 7 way into dev’s mind • We made all of our changes manually
  26. 26. SMALLER SITE OR TEST PROJECT • Ideally, do this compatibility update on a smaller site or test project first • Migrate to a PHP 7 server and see if there are any surprises • Now you’ll be ready to do the big move • Then you can take advantage of the new PHP 7 features finally!
  27. 27. PHP 7 IS WORTH IT • FASTER • They say it’s twice as fast, half as light • Stricter syntax and newer error handling – your code will get better over time • MySQL is faster too • I’m trying to learn more about PHP 7 while I’m at ZendCon!
  28. 28. QUESTIONS? • I will post slides later, follow @anngaff • Email questions: anntgaffigan@gmail.com

×