SlideShare a Scribd company logo
MAKINGTHE MOST
OF MODERN PHP
Ryan Szrama, Commerce Guys
DrupalCamp Asheville 2015
RUN FASTER,
JUMP HIGHER
… when you use PHP
the 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 aTaxonomy Web Service
API integration, taxonomy term
creation, and tag management.
My real world example:
Integrating aTaxonomy Web Service
Delivered as a PHP library integrated
into Drupal in 50% of the estimate.
MODERN PHP IS
OBJECT 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 SlimContainer();
$container['thing'] = function($container) {
return (object) array('doer' => 'done');
};
$app = new SlimApp($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 SlimApp();
$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 PHP
USES 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 onTwitter: @ryanszrama

More Related Content

PDF
Hello World on Slim Framework 3.x
PDF
Don't worry be API with Slim framework and Joomla
ODP
Javascript laravel's friend
PDF
Laravel Restful API and AngularJS
PDF
Web services with laravel
PDF
RESTful API development in Laravel 4 - Christopher Pecoraro
PPTX
Slim Framework
PPTX
Laravel 5
Hello World on Slim Framework 3.x
Don't worry be API with Slim framework and Joomla
Javascript laravel's friend
Laravel Restful API and AngularJS
Web services with laravel
RESTful API development in Laravel 4 - Christopher Pecoraro
Slim Framework
Laravel 5

What's hot (20)

PPT
Web service with Laravel
PPTX
Intro to Laravel
PPTX
Laravel Beginners Tutorial 1
PPTX
API Development with Laravel
PDF
Laravel 5 Annotations: RESTful API routing
KEY
Silex, the microframework
ODP
REST API Laravel
PPTX
Laravel 5
PPTX
Laravel ppt
PPTX
Laravel - Website Development in Php Framework.
PDF
Getting to know Laravel 5
PDF
Introduction to Laravel
PPTX
Laravel for Web Artisans
PDF
Laravel Introduction
PPT
What Is Hobo ?
PDF
Rest api titouan benoit
PDF
Why Laravel?
PDF
Rails Engines
PPTX
Laravel overview
PDF
Rails engines in large apps
Web service with Laravel
Intro to Laravel
Laravel Beginners Tutorial 1
API Development with Laravel
Laravel 5 Annotations: RESTful API routing
Silex, the microframework
REST API Laravel
Laravel 5
Laravel ppt
Laravel - Website Development in Php Framework.
Getting to know Laravel 5
Introduction to Laravel
Laravel for Web Artisans
Laravel Introduction
What Is Hobo ?
Rest api titouan benoit
Why Laravel?
Rails Engines
Laravel overview
Rails engines in large apps
Ad

Similar to Making the Most of Modern PHP in Drupal 7 (20)

PPTX
Ran Mizrahi - Symfony2 meets Drupal8
PDF
[HKDUG] #20161210 - BarCamp Hong Kong 2016 - What's News in PHP?
PDF
Drupal 8 - Core and API Changes
PDF
Drupal8 for Symfony Developers (PHP Day Verona 2017)
PDF
Modern Php New Features And Good Practices Josh Lockhart Lockhart
PDF
Introducing symfony2
PDF
Modern php
PDF
Lis651n12a.a4
PDF
Lis651n12a.a4
PDF
Lis651n12a.a4
PDF
Drupal 7 migrating to drupal 8
PPTX
Drupal
PDF
Modernize Your Drupal Development
PDF
Drupal8 for Symfony developers - Dutch PHP
PDF
Php and webservices
PDF
"Building Modern PHP Applications" - Jackson Murtha, South Dakota Code Camp 2012
PDF
PHP Standards Recommendations - PHP-FIG
PDF
Symfony and Drupal 8
PDF
WordPress Development in a Modern PHP World
PDF
PHP Web Development: Empowering the Digital World
Ran Mizrahi - Symfony2 meets Drupal8
[HKDUG] #20161210 - BarCamp Hong Kong 2016 - What's News in PHP?
Drupal 8 - Core and API Changes
Drupal8 for Symfony Developers (PHP Day Verona 2017)
Modern Php New Features And Good Practices Josh Lockhart Lockhart
Introducing symfony2
Modern php
Lis651n12a.a4
Lis651n12a.a4
Lis651n12a.a4
Drupal 7 migrating to drupal 8
Drupal
Modernize Your Drupal Development
Drupal8 for Symfony developers - Dutch PHP
Php and webservices
"Building Modern PHP Applications" - Jackson Murtha, South Dakota Code Camp 2012
PHP Standards Recommendations - PHP-FIG
Symfony and Drupal 8
WordPress Development in a Modern PHP World
PHP Web Development: Empowering the Digital World
Ad

More from Ryan Szrama (6)

PDF
Developing with Configuration Management on Drupal 7
PPT
Drupal Commerce at DrupalCon Chicago
PPT
Paris Commerce Sprint
PPTX
Drupal Commerce, DrupalCamp Colorado 2010
PPTX
10 Tips for E-commerce on Drupal
PPTX
Drupal Commerce, Web Content 2010
Developing with Configuration Management on Drupal 7
Drupal Commerce at DrupalCon Chicago
Paris Commerce Sprint
Drupal Commerce, DrupalCamp Colorado 2010
10 Tips for E-commerce on Drupal
Drupal Commerce, Web Content 2010

Recently uploaded (20)

PPTX
L1 - Introduction to python Backend.pptx
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PPTX
Reimagine Home Health with the Power of Agentic AI​
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PDF
Understanding Forklifts - TECH EHS Solution
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PPTX
CHAPTER 2 - PM Management and IT Context
PPTX
Essential Infomation Tech presentation.pptx
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PDF
System and Network Administration Chapter 2
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
Digital Strategies for Manufacturing Companies
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PPTX
Introduction to Artificial Intelligence
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PDF
AI in Product Development-omnex systems
L1 - Introduction to python Backend.pptx
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
Reimagine Home Health with the Power of Agentic AI​
Upgrade and Innovation Strategies for SAP ERP Customers
Which alternative to Crystal Reports is best for small or large businesses.pdf
Understanding Forklifts - TECH EHS Solution
How to Migrate SBCGlobal Email to Yahoo Easily
CHAPTER 2 - PM Management and IT Context
Essential Infomation Tech presentation.pptx
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
System and Network Administration Chapter 2
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PTS Company Brochure 2025 (1).pdf.......
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
Digital Strategies for Manufacturing Companies
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Introduction to Artificial Intelligence
wealthsignaloriginal-com-DS-text-... (1).pdf
AI in Product Development-omnex systems

Making the Most of Modern PHP in Drupal 7

  • 1. MAKINGTHE MOST OF MODERN PHP Ryan Szrama, Commerce Guys DrupalCamp Asheville 2015
  • 2. RUN FASTER, JUMP HIGHER … when you use PHP the right way.
 http://www.phptherightway.com
  • 3. • Deliver more capable projects in less time. • Improve the quality of your Drupal modules. • Prepare yourself / your team for Drupal 8.
  • 4. My real world example: Integrating aTaxonomy Web Service API integration, taxonomy term creation, and tag management.
  • 5. My real world example: Integrating aTaxonomy Web Service Delivered as a PHP library integrated into Drupal in 50% of the estimate.
  • 6. MODERN PHP IS OBJECT ORIENTED • Interfaces, Classes, Inheritance, Namespaces,Traits • Class autoloading based on namespaces • Object oriented design patterns are everywhere; for example, consider Dependency Injection…
  • 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();
  • 8. <?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);
  • 9. <?php // Quick example of a dependency injection // container in a Slim 3.x app. include 'vendor/autoload.php'; $container = new SlimContainer(); $container['thing'] = function($container) { return (object) array('doer' => 'done'); }; $app = new SlimApp($container); echo $app->thing->doer;
  • 10. 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.)
  • 11. 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
  • 12. 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
  • 13. <?php // A smaller codebase than Drupal’s is // easier to digest while learning. require 'vendor/autoload.php'; $app = new SlimApp(); $app->get('/', function($request, $response, $args) { echo 'Hello, world!'; }); $app->run();
  • 14. 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.
  • 15. 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)
  • 16. MODERN PHP USES 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.
  • 17. { “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” } } }
  • 18. 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.
  • 19. 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.
  • 20. 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.
  • 21. 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!
  • 22. 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.
  • 23. Learn and adopt the language improvements, tools, and design principles of modern PHP to become a better Drupal developer today. Questions? Find me onTwitter: @ryanszrama