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

621 views

Published on

Presented at WordCamp Birmingham, August 2019.

Published in: Education
  • 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/wpyall19
  6. 6. WHAT DOES MODERN PHP DEVELOPMENT REALLY MEAN?
  7. 7. POWERFUL
  8. 8. WHAT DOES MODERN PHP DEVELOPMENT MEAN? MODERN PHP 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
  9. 9. MODERN DOESN’T JUST MEAN NEWER FEATURES
  10. 10. FOLLOW THE LEADER?
  11. 11. “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
  12. 12. Slides: http://drewf.us/wpyall19
  13. 13. THE CONCEPTS
  14. 14. REFACTORING OLD CODE
  15. 15. BEST PRACTICES REFACTORING APPROACHES ▸ All or nothing ▸ Rewrite ▸ Back-compat concerns ▸ Gradual iteration ▸ Partial activation ▸ Version-protected bootstrap
  16. 16. REFACTORING FOR THE SAKE OF REFACTORING: Proceed with Caution
  17. 17. NAVIGATING NEW SKILLS TAKES TIME
  18. 18. DO: DIVE IN
 HEAD FIRST
  19. 19. DON’T: JAM UP
 YOUR USERS
  20. 20. EMBRACE WHAT YOUR USERS CAN (VS SHOULD) SUPPORT
  21. 21. NAMESPACES
  22. 22. BEST PRACTICES NAMESPACES ▸ Establishes baseline code ownership ▸ Simpler naming of allthethings ▸ Better code segregation ▸ Easier interoperability with other people’s code
  23. 23. NAMESPACES EXAMPLES ▸ My_Verbose_Plugin_Class_Name ▸ My_PluginClass_Name ▸ My_PluginComponentClass_Name ▸ My_Pluginfunction_name() ▸ ThisTotallyWorksEvenThoughItIsRidiculous
  24. 24. NAMESPACES IN PRACTICE: BEFORE
  25. 25. NAMESPACES IN PRACTICE: AFTER
  26. 26. WHY USE NAMESPACES?
  27. 27. POWERFUL
  28. 28. BEST PRACTICES WHY USE NAMESPACES? ▸ Prevent naming collisions ▸ Code readability ▸ Promote better code organization ▸ Aliasing can help marry legacy with modern code
  29. 29. WITH GREAT ALIASING POWER COMES GREAT RESPONSIBILITY
  30. 30. 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 );
  31. 31. 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 );
  32. 32. NAMESPACES ALIASING IN PRACTICE: BEFORE
  33. 33. NAMESPACES ALIASING IN PRACTICE: AFTER
  34. 34. NAMESPACES ALIASING IN PRACTICE: AFTER
  35. 35. AUTOLOADING WHAT YOU NEED WHEN YOU NEED IT
  36. 36. 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
  37. 37. AUTOLOADERS CONVERT
 CLASS NAMES
 INTO FILE NAMES
  38. 38. BEST PRACTICES WHY USE NAMESPACES WITH AN AUTOLOADER? ▸ ComponentSub_ComponentFile ▸ Component -> Sub_Component -> File.php
  39. 39. POWERFUL
  40. 40. AUTOLOADING AUTOLOADING IN PRACTICE
  41. 41. 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
  42. 42. HINTING AT MISTAKES BEFORE YOU MAKE THEM
  43. 43. 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
  44. 44. TYPE HINTS EXAMPLES ▸ function feed_pet( Pet $pet ) ▸ ex_update_collection( Collection $collection, array $atts ); ▸ try {} catch( Marker_Interface $exception ) {}
  45. 45. TYPE HINTS IN PRACTICE
  46. 46. TYPE HINTS IN PRACTICE
  47. 47. NICE TO HAVES
  48. 48. AVOID RUNNING OVER YOUR USERS IN THE PURSUIT OF SHINY THINGS
  49. 49. NICE TO HAVES CLOSURES (5.3+) ▸ Anonymous functions ▸ One way to express simple one-time-use code ▸ Be careful using closures with hooks
  50. 50. 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 );
  51. 51. 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 );
  52. 52. 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 );
  53. 53. CLOSURES IN PRACTICE
  54. 54. CLOSURES IN PRACTICE
  55. 55. CLOSURES CAN BE HANDY, BUT SHOULD BE USED WITH CARE
  56. 56. FLIPPING OOP
 ON ITS HEAD
  57. 57. 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
  58. 58. LATE STATIC BINDING EXAMPLES
  59. 59. LATE STATIC BINDING EXAMPLES
  60. 60. RECYCLING CODE
  61. 61. 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)
  62. 62. TRAITS IN PRACTICE: BEFORE
  63. 63. TRAITS IN PRACTICE: BEFORE
  64. 64. TRAITS IN PRACTICE: AFTER
  65. 65. EFFICIENT ALIASING
  66. 66. 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
  67. 67. GROUP ALIASING EXAMPLES
  68. 68. GROUP ALIASING IN PRACTICE
  69. 69. BACK-COMPAT GOTCHAS
  70. 70. 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+)
  71. 71. “SOFTWARE IS A GREAT COMBINATION BETWEEN ARTISTRY AND ENGINEERING.” Bill Gates WORDPRESS DEVELOPMENT IN A MODERN PHP WORLD
  72. 72. THANK YOU Drew Jaynes (DrewAPicture)
 Slides: http://drewf.us/wpyall19

×