WORDPRESS DEVELOPMENT
IN A MODERN PHP WORLD
DREW JAYNES
DREW JAYNES
SANDHILLS DEVELOPMENT
THIS TALK:

WHY VS HOW
THIS TALK
TALK STRUCTURE
▸ Basic benefits of each concept
▸ Why each is worth pursuing
▸ Pseudo-code examples
▸ Practical examples
Slides: http://drewf.us/wpyall19
WHAT DOES MODERN PHP
DEVELOPMENT REALLY MEAN?
POWERFUL
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
MODERN DOESN’T JUST
MEAN NEWER FEATURES
FOLLOW THE LEADER?
“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
Slides: http://drewf.us/wpyall19
THE CONCEPTS
REFACTORING
OLD CODE
BEST PRACTICES
REFACTORING APPROACHES
▸ All or nothing
▸ Rewrite
▸ Back-compat concerns
▸ Gradual iteration
▸ Partial activation
▸ Version-protected bootstrap
REFACTORING FOR THE
SAKE OF REFACTORING:
Proceed with Caution
NAVIGATING NEW
SKILLS TAKES TIME
DO: DIVE IN

HEAD FIRST
DON’T: JAM UP

YOUR USERS
EMBRACE WHAT YOUR USERS
CAN (VS SHOULD) SUPPORT
NAMESPACES
BEST PRACTICES
NAMESPACES
▸ Establishes baseline code ownership
▸ Simpler naming of allthethings
▸ Better code segregation
▸ Easier interoperability with other people’s code
NAMESPACES
EXAMPLES
▸ My_Verbose_Plugin_Class_Name
▸ My_PluginClass_Name
▸ My_PluginComponentClass_Name
▸ My_Pluginfunction_name()
▸ ThisTotallyWorksEvenThoughItIsRidiculous
NAMESPACES
IN PRACTICE: BEFORE
NAMESPACES
IN PRACTICE: AFTER
WHY USE
NAMESPACES?
POWERFUL
BEST PRACTICES
WHY USE NAMESPACES?
▸ Prevent naming collisions
▸ Code readability
▸ Promote better code organization
▸ Aliasing can help marry legacy with modern code
WITH GREAT ALIASING POWER
COMES GREAT RESPONSIBILITY
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 );
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 );
NAMESPACES
ALIASING IN PRACTICE: BEFORE
NAMESPACES
ALIASING IN PRACTICE: AFTER
NAMESPACES
ALIASING IN PRACTICE: AFTER
AUTOLOADING WHAT YOU
NEED WHEN YOU NEED IT
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
AUTOLOADERS CONVERT

CLASS NAMES

INTO FILE NAMES
BEST PRACTICES
WHY USE NAMESPACES WITH AN AUTOLOADER?
▸ ComponentSub_ComponentFile
▸ Component -> Sub_Component -> File.php
POWERFUL
AUTOLOADING
AUTOLOADING IN PRACTICE
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
HINTING AT MISTAKES
BEFORE YOU MAKE THEM
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
TYPE HINTS
EXAMPLES
▸ function feed_pet( Pet $pet )
▸ ex_update_collection( Collection $collection, array $atts );
▸ try {} catch( Marker_Interface $exception ) {}
TYPE HINTS
IN PRACTICE
TYPE HINTS
IN PRACTICE
NICE TO HAVES
AVOID RUNNING OVER YOUR USERS
IN THE PURSUIT OF SHINY THINGS
NICE TO HAVES
CLOSURES (5.3+)
▸ Anonymous functions
▸ One way to express simple one-time-use code
▸ Be careful using closures with hooks
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 );
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 );
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 );
CLOSURES
IN PRACTICE
CLOSURES
IN PRACTICE
CLOSURES CAN BE HANDY, BUT
SHOULD BE USED WITH CARE
FLIPPING OOP

ON ITS HEAD
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
LATE STATIC BINDING
EXAMPLES
LATE STATIC BINDING
EXAMPLES
RECYCLING CODE
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)
TRAITS
IN PRACTICE: BEFORE
TRAITS
IN PRACTICE: BEFORE
TRAITS
IN PRACTICE: AFTER
EFFICIENT ALIASING
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
GROUP ALIASING
EXAMPLES
GROUP ALIASING
IN PRACTICE
BACK-COMPAT
GOTCHAS
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+)
“SOFTWARE IS A GREAT COMBINATION
BETWEEN ARTISTRY AND ENGINEERING.”
Bill Gates
WORDPRESS DEVELOPMENT IN A MODERN PHP WORLD
THANK YOU
Drew Jaynes (DrewAPicture)

Slides: http://drewf.us/wpyall19

WordPress Development in a Modern PHP World