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.

WordPress Development in a Modern PHP World

204 views

Published on

WordPress’ minimum-supported version is still PHP 5.2 (gasp!) but that doesn’t mean you should let that hold you back from leveraging modern PHP features in your plugins and themes!

In this talk, we’ll take a deep dive into ways you can start leveraging modern PHP features today to bring new life to that old plugin or theme. We’ll cover how to code partial plugin activation for old versions of PHP, use autoloaders and namespaces, setup group aliasing, use return type declarations, leverage traits, and much more.

Published in: Engineering
  • Be the first to comment

  • Be the first to like this

WordPress Development in a Modern PHP World

  1. 1. WORDPRESS DEVELOPMENT IN A MODERN PHP WORLD DREW JAYNES
  2. 2. DREW JAYNES SANDHILLS DEVELOPMENT
  3. 3. THIS TALK:
 WHY VS HOW
  4. 4. THIS TALK TALK STRUCTURE ▸ Basic benefits of each concept ▸ Why each is worth pursuing ▸ Pseudo-code examples ▸ Practical examples
  5. 5. Slides: http://drewf.us/wcphx19
  6. 6. WHAT DOES MODERN PHP DEVELOPMENT REALLY MEAN?
  7. 7. WHAT DOES MODERN PHP DEVELOPMENT MEAN? MODERN PHP DEVELOPMENT CONCEPTS ▸ Namespaces ▸ Autoloading ▸ Closures (anonymous functions) ▸ *Type hints (and return type declarations) ▸ Late static binding ▸ Traits ▸ Generators ▸ Group aliasing ▸ *Exception-based error handling ▸ libsodium and modern security
  8. 8. MODERN DOESN’T JUST MEAN NEWER FEATURES
  9. 9. FOLLOW THE LEADER?
  10. 10. “AS FOR THE FUTURE, YOUR TASK IS NOT TO FORESEE IT, BUT TO ENABLE IT." Antoine De Saint-Exupéry WORDPRESS DEVELOPMENT IN A MODERN PHP WORLD
  11. 11. Slides: http://drewf.us/wcphx19
  12. 12. THE CONCEPTS
  13. 13. REFACTORING OLD CODE
  14. 14. BEST PRACTICES REFACTORING APPROACHES ▸ All or nothing ▸ Rewrite ▸ Back-compat concerns ▸ Gradual iteration ▸ Partial activation ▸ Version-protected bootstrap
  15. 15. REFACTORING FOR THE SAKE OF REFACTORING: A cautionary tale
  16. 16. NAVIGATING NEW SKILLS TAKES TIME
  17. 17. DO: DIVE IN
 HEAD FIRST
  18. 18. DON’T: JAM UP
 YOUR USERS
  19. 19. EMBRACE WHAT YOUR USERS CAN SUPPORT
  20. 20. NAMESPACES
  21. 21. BEST PRACTICES NAMESPACES ▸ Establishes baseline code ownership ▸ Simpler naming of allthethings ▸ Better code segregation ▸ Easier interoperability with other people’s code
  22. 22. NAMESPACES EXAMPLES ▸ My_Verbose_Plugin_Class_Name ▸ My_PluginClass_Name ▸ My_PluginComponentClass_Name ▸ My_Pluginfunction_name() ▸ ThisTotallyWorksEvenThoughItsRidiculous
  23. 23. NAMESPACES IN PRACTICE: BEFORE
  24. 24. NAMESPACES IN PRACTICE: AFTER
  25. 25. WHY USE NAMESPACES?
  26. 26. BEST PRACTICES WHY USE NAMESPACES? ▸ Prevent naming collisions ▸ Code readability ▸ Promote better code organization ▸ Aliasing can help marry legacy with modern code
  27. 27. WITH GREAT ALIASING POWER COMES GREAT RESPONSIBILITY
  28. 28. NAMESPACES ALIASING EXAMPLES ▸ use Legacy_Verbose_Controller_Class as Standardized_Controller;
 
 class Controller extends Standardized_Controller {} ▸ class_alias( ‘Legacy_Controller_Class’, ‘ComponentStandardized_Controller’ );
 
 new ComponentStandardized_Controller( $collection );
  29. 29. NAMESPACES ALIASING EXAMPLES ▸ use Legacy_Verbose_Controller_Class as Standardized_Controller;
 
 class Controller extends Standardized_Controller {} ▸ class_alias( ‘Legacy_Controller_Class’, ‘ComponentStandardized_Controller’ );
 
 new ComponentStandardized_Controller( $collection );
  30. 30. NAMESPACES ALIASING IN PRACTICE: BEFORE
  31. 31. NAMESPACES ALIASING IN PRACTICE: AFTER
  32. 32. NAMESPACES ALIASING IN PRACTICE: AFTER
  33. 33. AUTOLOADING WHAT YOU NEED WHEN YOU NEED IT
  34. 34. BEST PRACTICES AUTOLOADING ▸ Automatic loading of classes etc. instead of manually requiring them ▸ Typically follows project organization ▸ Composer vs PSR-0 vs PSR-4 vs Rolling your own
  35. 35. AUTOLOADERS CONVERT
 CLASS NAMES
 INTO FILE NAMES
  36. 36. AUTOLOADING AUTOLOADING IN PRACTICE
  37. 37. AUTOLOADING RESOURCES ▸ Composer: https://getcomposer.org/ ▸ PSR-0: https://www.php-fig.org/psr/psr-0/ ▸ PSR-4: https://www.php-fig.org/psr/psr-4/ ▸ Drew’s autoloader: https://gist.github.com/DrewAPicture/ b0f1c0e637b072a41e5dfec6a38a7808 ▸ Tom McFarlin’s autoloader: https://github.com/tommcfarlin/simple- autoloader-for-wordpress
  38. 38. HINTING AT MISTAKES BEFORE YOU MAKE THEM
  39. 39. WORDPRESS DEVELOPMENT IN A MODERN PHP WORLD TYPE HINTS ▸ Helps catch avoidable mistakes ▸ Defines expectations for what is accepted ▸ *Base support for class/interface name or self ▸ 5.1 added array, 5.4 added callable ▸ 7.0 added scalar type support,
 e.g. string, bool, int, float (no aliases!) ▸ 7.1 added iterable, 7.2 added object ▸ Multiple types not supported
  40. 40. TYPE HINTS EXAMPLES ▸ function feed_pet( Pet $pet ) ▸ ex_update_collection( Collection $collection, array $atts ); ▸ try {} catch( Marker_Interface $exception ) {}
  41. 41. TYPE HINTS IN PRACTICE
  42. 42. TYPE HINTS IN PRACTICE
  43. 43. NICE TO HAVES
  44. 44. AVOID RUNNING OVER YOUR USERS IN THE PURSUIT OF SHINY THINGS
  45. 45. NICE TO HAVES CLOSURES (5.3+) ▸ Anonymous functions ▸ One way to express simple one-time-use code ▸ Be careful using closures with hooks
  46. 46. CLOSURES PANDORA’S ANONYMOUS BOX ▸ add_action( ‘some_hook’, function( $param1, $param2 ) {
 // do stuff
 } ▸ function feed_pet( callable $process ) {} ▸ feed_pet( function() {
 echo ‘Pet is fed.’;
 } ); ▸ call_user_func_array( $callback, $args );
  47. 47. CLOSURES PANDORA’S ANONYMOUS BOX ▸ add_action( ‘some_hook’, function( $param1, $param2 ) {
 // do stuff
 } ▸ function feed_pet( callable $message ) {} ▸ feed_pet( function() {
 echo ‘My pet has been fed.’;
 } ); ▸ call_user_func_array( $callback, $args );
  48. 48. CLOSURES PANDORA’S ANONYMOUS BOX ▸ add_action( ‘some_hook’, function( $param1, $param2 ) {
 // do stuff
 } ▸ function feed_pet( callable $process ) {} ▸ feed_pet( function() {
 echo ‘Pet is fed.’;
 } ); ▸ call_user_func_array( $callback, $args );
  49. 49. CLOSURES IN PRACTICE
  50. 50. CLOSURES IN PRACTICE
  51. 51. CLOSURES CAN BE HANDY, BUT SHOULD BE USED WITH CARE
  52. 52. FLIPPING OOP
 ON ITS HEAD
  53. 53. NICE TO HAVES LATE STATIC BINDING (5.3+) ▸ Allows parent/super classes to statically reference aspects
 of the calling or sub-class ▸ Flips the OOP inheritance model on its head ▸ Leveraged via the static keyword vs self
  54. 54. LATE STATIC BINDING EXAMPLES
  55. 55. LATE STATIC BINDING EXAMPLES
  56. 56. RECYCLING CODE
  57. 57. NICE TO HAVES TRAITS (5.4+) ▸ Makes reusing code across classes simpler ▸ Free of inheritance (horizontal use) ▸ Traits vs dependency injection ▸ Really powerful when combined with abstract methods ▸ Are traits really evil? (no)
  58. 58. TRAITS IN PRACTICE: BEFORE
  59. 59. TRAITS IN PRACTICE: BEFORE
  60. 60. TRAITS IN PRACTICE: AFTER
  61. 61. EFFICIENT ALIASING
  62. 62. NICE TO HAVES GROUP ALIASING (7+) ▸ Allows for aliasing multiple classes or functions at once ▸ Cuts down on top-heavy aliasing blocks ▸ Importing and aliasing functions, importing constants was added in 5.6
  63. 63. GROUP ALIASING EXAMPLES
  64. 64. GROUP ALIASING IN PRACTICE
  65. 65. BACK-COMPAT GOTCHAS
  66. 66. GOTCHAS BACK-IN-COMPAT TO LOOK OUT FOR ▸ Call-time pass by reference (warning 5.3, fatal 5.4) ▸ my_update_function( &$something ) // WRONG ▸ PHP4-style constructors deprecated (7.0+) ▸ Static calls to non-static methods (deprecated 7.0+)
  67. 67. “SOFTWARE IS A GREAT COMBINATION BETWEEN ARTISTRY AND ENGINEERING.” Bill Gates WORDPRESS DEVELOPMENT IN A MODERN PHP WORLD
  68. 68. THANK YOU Drew Jaynes | DrewAPicture
 Slides: http://drewf.us/wcphx19

×