PHP Week 8 INFM 603. Agenda Questions PHP Drupal Project Plan.
Making the Most of Modern PHP in Drupal 7
-
Upload
ryan-szrama -
Category
Software
-
view
951 -
download
0
Transcript of Making the Most of Modern PHP in Drupal 7
MAKING THE MOSTOF MODERN PHP
Ryan Szrama, Commerce GuysDrupalCamp Asheville 2015
RUN FASTER,JUMP HIGHER
… when you use PHPthe right way.
http://www.phptherightway.com
• Deliver more capable projects in less time.
• Improve the quality of your Drupal modules.
• Prepare yourself / your team for Drupal 8.
My real world example:Integrating a Taxonomy Web Service
API integration, taxonomy termcreation, and tag management.
My real world example:Integrating a Taxonomy Web Service
Delivered as a PHP library integratedinto Drupal in 50% of the estimate.
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…
<?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();
<?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);
<?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;
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.)
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
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
<?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();
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.
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)
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.
{ “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\” } } }
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.
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.
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.
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!
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.
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