Making the Most of Modern PHP in Drupal 7

23
MAKING THE MOST OF MODERN PHP Ryan Szrama, Commerce Guys DrupalCamp Asheville 2015

Transcript of Making the Most of Modern PHP in Drupal 7

Page 1: Making the Most of Modern PHP in Drupal 7

MAKING THE MOSTOF MODERN PHP

Ryan Szrama, Commerce GuysDrupalCamp Asheville 2015

Page 2: Making the Most of Modern PHP in Drupal 7

RUN FASTER,JUMP HIGHER

… when you use PHPthe right way.

http://www.phptherightway.com

Page 3: Making the Most of Modern PHP in Drupal 7

• Deliver more capable projects in less time.

• Improve the quality of your Drupal modules.

• Prepare yourself / your team for Drupal 8.

Page 4: Making the Most of Modern PHP in Drupal 7

My real world example:Integrating a Taxonomy Web Service

API integration, taxonomy termcreation, and tag management.

Page 5: Making the Most of Modern PHP in Drupal 7

My real world example:Integrating a Taxonomy Web Service

Delivered as a PHP library integratedinto Drupal in 50% of the estimate.

Page 6: Making the Most of Modern PHP in Drupal 7

MODERN PHP ISOBJECT ORIENTED

• Interfaces, Classes, Inheritance, Namespaces, Traits

• Class autoloading based on namespaces

• Object oriented design patterns are everywhere; for example, consider Dependency Injection…

Page 7: Making the Most of Modern PHP in Drupal 7

<?php

// In this case, Cars can only use // a single type of engine.

class Car { protected $engine;

function __construct() { $this->engine = new Engine(); } }

$car = new Car();

Page 8: Making the Most of Modern PHP in Drupal 7

<?php

class Car { protected $engine;

function __construct($engine) { $this->engine = $engine; } }

// Use whatever engine you want. $engine = new NormalEngine(); $car = new Car($engine);

$engine = new TurboEngine(); $turboCar = new Car($engine);

Page 9: Making the Most of Modern PHP in Drupal 7

<?php

// Quick example of a dependency injection // container in a Slim 3.x app.

include 'vendor/autoload.php';

$container = new \Slim\Container();

$container['thing'] = function($container) { return (object) array('doer' => 'done'); };

$app = new \Slim\App($container);

echo $app->thing->doer;

Page 10: Making the Most of Modern PHP in Drupal 7

MODERN PHP USES INTEROPERABLE LIBRARIES

• The PHP Framework Interoperability Group (PHP-FIG) discusses and ratifies PHP Standards Recommendations (PSRs).

• PSRs are independent standards, not successive levels of compliance. (e.g. PSR-4 deprecates PSR-0, and a project can be PSR-2 without anything else.)

Page 11: Making the Most of Modern PHP in Drupal 7

MODERN PHP USES INTEROPERABLE LIBRARIES

• PSR-4 (or PSR-0): Autoloading

• PSR-1 / PSR-2: Coding standards

• PSR-3: Logging interface

• PSR-7: HTTP Request and Response interfaces

• Learn more at: http://www.php-fig.org

Page 12: Making the Most of Modern PHP in Drupal 7

See them in use in Slim 3.x, created by Josh Lockhart (a.k.a. @codeguy), author of Modern PHP

and PHP the Right Way.http://www.phptherightway.com

Page 13: Making the Most of Modern PHP in Drupal 7

<?php

// A smaller codebase than Drupal’s is // easier to digest while learning.

require 'vendor/autoload.php';

$app = new \Slim\App();

$app->get('/', function($request, $response, $args) { echo 'Hello, world!'; });

$app->run();

Page 14: Making the Most of Modern PHP in Drupal 7

Read more: http://ryanszrama.com/topics/slim

In fact, learning to develop with Slim 3.x helped me better understand essential concepts in Drupal 8.

Page 15: Making the Most of Modern PHP in Drupal 7

COMMERCE 2.X IS BUILT ON STANDALONE LIBRARIES

• commerceguys/intl (Currency formatting)

• commerceguys/addressing (Address formatting)

• commerceguys/zone (Address grouping)

• commerceguys/enum (Enumeration data structure)

• commerceguys/tax (Tax management)

Page 16: Making the Most of Modern PHP in Drupal 7

MODERN PHPUSES COMPOSER

• Dependency management tool; functionally equivalent to .info files + drush make.

• Project information is sourced from packagist.org.

• Excellent built-in support for Packagist in GitHub.

Page 17: Making the Most of Modern PHP in Drupal 7

{ “name”: “rszrama/negotiation-middleware”, “type”: “library”, “license”: “MIT”, “require”: { “php”: “>=5.4.0”, “psr/http-message”: “1.0”, “willdurand/negotiation”: “2.0.0-alpha1” }, “autoload”: { “psr-4”: { “NegotiationMiddleware\\”: “src\” } } }

Page 18: Making the Most of Modern PHP in Drupal 7

https://www.previousnext.com.au/blog/drupal-8-now-phpunit-tests-drupal-7

Bonus points: dig into PHPUnit to automate unit testing your code… even on Drupal 7.

Page 19: Making the Most of Modern PHP in Drupal 7

MODERN PHP IN DRUPAL 7

• Write API integrations and data modeling / manipulation functions as standalone libraries.

• Ensure those libraries do not depend on Drupal.

• Use adapter classes or dependency injection to make use of that code inside of Drupal.

Page 20: Making the Most of Modern PHP in Drupal 7

MODERN PHP IN DRUPAL 7

• Drupal 7 only requires PHP 5.2.x, so its core autoloading API does not support namespaces.

• Instead use Composer / the Composer Manager module to manage your project dependencies.

• At least use PSR-4; to reach the larger PHP world, consider using PSR-2 and the MIT license.

Page 21: Making the Most of Modern PHP in Drupal 7

MODERN PHP IN DRUPAL 7

• Use the Standard PHP Library’s exception classes or create your own more specific exceptions.

• Use try / catch to avoid crippling fatal errors… EntityMetadataWrapper exceptions, anyone?

• Speaking of the SPL, check out the various data structures it provides out of the box!

Page 22: Making the Most of Modern PHP in Drupal 7

http://www.slimframework.com

If your project has components that don’t require a full Drupal bootstrap, consider Slim 3.x or a similar

micro-framework to get off the Drupal island.

Page 23: Making the Most of Modern PHP in Drupal 7

Learn and adopt the language improvements, tools, and design principles of modern PHP to become a

better Drupal developer today.

Questions? Find me on Twitter : @ryanszrama