"building modern php applications" - jackson murtha, south dakota code camp 2012

41
Building Modern PHP Applications How PHP language, tool, and framework advancements will help you improve your applications.

Upload: blend-interactive

Post on 08-May-2015

9.470 views

Category:

Documents


0 download

DESCRIPTION

"Building Modern PHP Applications" by Jackson Murtha, given November 10, 2012, at South Dakota Code Camp 2012 in Sioux Falls.

TRANSCRIPT

Page 1: "Building Modern PHP Applications" - Jackson Murtha, South Dakota Code Camp 2012

Building Modern PHP Applications

How PHP language, tool, and framework advancements will help you improve your

applications.

Page 2: "Building Modern PHP Applications" - Jackson Murtha, South Dakota Code Camp 2012

Me

● Developer at Blend Interactive (blendinteractive.com)

● Sioux Falls, SD● Contact Me:

– shellrunner.com

– @jacksonmurtha

– github.com/jxn

– gplus.to/jacksonmurtha

[email protected]

Page 3: "Building Modern PHP Applications" - Jackson Murtha, South Dakota Code Camp 2012

Attendee Survey

● Are you developing applications in PHP?– Which version? Which Platform(s)?

– Are you using a framework?

● What other languages are you using?● Are you testing your PHP applications?

– Unit testing? Browser integration testing?

Page 4: "Building Modern PHP Applications" - Jackson Murtha, South Dakota Code Camp 2012

PHP's Image Problem

● Language origins● Deprecated features● Disorganized/disparate community● Poor release-planning● Persistent myths● Bad code (in high-profile applications)● New features are not well known● Some legitimate concerns (threads, enforced

security policies, naming / param consistency)

Page 5: "Building Modern PHP Applications" - Jackson Murtha, South Dakota Code Camp 2012

PHP Myths● OOP is difficult or impossible● Performance problems● Windows is a second-class citizen● Community in decline● Difficult/Impossible to separate

responsibilities (view-code littered with business logic)

● Not testable● Tools are focused on the document-web● No longer needed given client-side state● Dumb PHP “features” get in the way● New deployment methods are not

available (e.g. Heroku)● Poorly-managed releases, slow release

cycle

● Not a “real” programming language – a preprocessor

● Insecure● All “blog code”● Immature platform or only immature

applications● Poor garbage collection● Tools are outdated● Everything is global● Fate of the language is tied to the fate of

mod_php● No modern language features● Not enterprise-friendly● Difficult to add contributions● No visibility of roadmap

Page 6: "Building Modern PHP Applications" - Jackson Murtha, South Dakota Code Camp 2012

Valid Criticisms

● Naming / parameter consistency*● Multi-byte strings can cause problems*● Threading*● Few “forced” conventions*● PHP is rarely “first to feature”*● ???

Page 7: "Building Modern PHP Applications" - Jackson Murtha, South Dakota Code Camp 2012

Changes to the web

● emergence of “big data”● multi-server environments● viability of non-shared hosting● rise of the web application and decline

of the document web● popularization of rails/django● viability of compiled languages (C# /

Java)● growth of the mobile web● alternatives to database for storage● commercialization of

software/developer tools and training

● Javascript is now viable / sexy● ajax is ubiquitous● rising application complexity● client / browser enhancements● ascendancy of the web API● new code lifespan requirements● popularity of agile development● automated application testing● event-driven designs● virtualized environment advancements● return of the generated static page● new expectations and legal compliance● web development buzz and hype

Page 8: "Building Modern PHP Applications" - Jackson Murtha, South Dakota Code Camp 2012

PHP is constantly evolving

● Release / cultural changes

● Feature removal● New PHP features● New PHP tools● Second generation

frameworks● New external tools

Page 9: "Building Modern PHP Applications" - Jackson Murtha, South Dakota Code Camp 2012

Release / core development changes● New developers on the core team● New RFC process● Reworked community resources● Feature-release timetable, ~1 year● Bugfix / maintenance timetable● Bug reporting improvements, reworked documentation approval

process● Clearer expectations, more development visibility, smoother release

transitions, greater predictability● Larger, more diverse developer community and a more experienced

security team● Inter-framework cooperation & standards organizations

Page 10: "Building Modern PHP Applications" - Jackson Murtha, South Dakota Code Camp 2012

Framework Interop

● http://php-fig.org● Growing influence, with three standards● PSR-0

– Autoloading/namespacing

● PSR-1– Basic coding standard

● PSR-2– Thorough style-guide

● More coming....

Page 11: "Building Modern PHP Applications" - Jackson Murtha, South Dakota Code Camp 2012

Feature removal

● Removal of some of the features caused the worst code offenses and security / predictability problems– Magic Quotes

– register_globals

– safe_mode

– outdated mysql functions

Page 12: "Building Modern PHP Applications" - Jackson Murtha, South Dakota Code Camp 2012

New Features – PHP 5.0 – 5.3

● Improved inheritance model● Namespaces● PDO● Closures● Phar● Exceptions● SPL work / extensions improvements● Late static binding● Garbage collector improvements

Page 13: "Building Modern PHP Applications" - Jackson Murtha, South Dakota Code Camp 2012

Namespaces

● Encapsulate Classes, Interfaces, Functions, Constants, Traits

● Provide grouping, prevent collisions, allow aliasing

● Encourage segregation of code

● Replace hackish alternatives

● Compile-time, not runtime

Page 14: "Building Modern PHP Applications" - Jackson Murtha, South Dakota Code Camp 2012

PDO

● PHP Data Objects● “data-access” abstraction layer● Provide for object-oriented database access● Encourage prepared statements:

Page 15: "Building Modern PHP Applications" - Jackson Murtha, South Dakota Code Camp 2012

Phar

● Single-file PHP archive● Executable package● Can access assets within and outside itself● phar:// stream wrapper available

Page 16: "Building Modern PHP Applications" - Jackson Murtha, South Dakota Code Camp 2012

Closures / Lambda functions

● Hijacked from functional languages

● More efficient than create_function()

● Very useful for sort(), array_map(), array_filter(), or function using a callback parameter.

Page 17: "Building Modern PHP Applications" - Jackson Murtha, South Dakota Code Camp 2012

Garbage Collection / Late Static Binding / Performance and memory enhancements

● Efficiency gains● Don't try to outsmart the compiler

Page 18: "Building Modern PHP Applications" - Jackson Murtha, South Dakota Code Camp 2012

New Features – 5.4

● upload progress● binary notation● array short syntax● array dereferencing● class member access

on instantiation● indirect method call by

array var

● $this in closures● callable typehint● jsonSerializable

interface● short echo tag always

available● built-in webserver● traits

Page 19: "Building Modern PHP Applications" - Jackson Murtha, South Dakota Code Camp 2012

Array Dereferencing

Page 20: "Building Modern PHP Applications" - Jackson Murtha, South Dakota Code Camp 2012

Built-in webserver

● Easy / no configuration in most environments● Development-only● To start: php -S localhost:8000

● Provides many server environment variables● Configuration php.ini, port, docroot can be

changed at execution

Page 21: "Building Modern PHP Applications" - Jackson Murtha, South Dakota Code Camp 2012

Traits

● Horizontal code-reuse (cf. mixins)

● “Compiler-assisted copy and paste”

● Obey most class behaviors, but cannot be instantiated

Page 22: "Building Modern PHP Applications" - Jackson Murtha, South Dakota Code Camp 2012

● Traits can be composed of other traits

● Aliasing and collision prevention with 'as' and 'insteadof'

● Multiple traits can be used at once

Page 23: "Building Modern PHP Applications" - Jackson Murtha, South Dakota Code Camp 2012

New Features – 5.5

● Password API● Setters / Getters● Generators● Intl improvements● array_column()● Fully-qualified class names● Try / Catch / Finally● Parameter skipping● Read-only properties● Named Parameters?

Page 24: "Building Modern PHP Applications" - Jackson Murtha, South Dakota Code Camp 2012

Generators

● Simpler method for working with iterators● Nearly identical to the python implementation

Page 25: "Building Modern PHP Applications" - Jackson Murtha, South Dakota Code Camp 2012

Setters / Getters

Page 26: "Building Modern PHP Applications" - Jackson Murtha, South Dakota Code Camp 2012

Password API

Page 27: "Building Modern PHP Applications" - Jackson Murtha, South Dakota Code Camp 2012

New PHP Tools

● Package tools– Composer (Pyrus/Pear2, Pear, Pecl, extensions,

Pirum)

● Testing tools– PHPUnit, Behat, Mink, php-selenium, goutte,

phpspec

● Static analysis tools– Php_Depend, phpcpd, phpdcd, codecoverage,

codesniffer, mess detector, PHP-CS-Fixer

Page 28: "Building Modern PHP Applications" - Jackson Murtha, South Dakota Code Camp 2012

Composer

● http://getcomposer.org● Repository management through packagist.org, bitbucket,

github, etc.● Solves many issues with PEAR● easy install: ● curl -s https://getcomposer.org/installer | php

● Package lists, sources, dependencies, updates, installation locations

● easy package setup, simple hosting setup, vcs/pear integration, github/bitbucket API tools, satis integration

● library autoloading via require 'vendor/autoload.php';

Page 29: "Building Modern PHP Applications" - Jackson Murtha, South Dakota Code Camp 2012

Composer.json

Page 30: "Building Modern PHP Applications" - Jackson Murtha, South Dakota Code Camp 2012

Second Generation Frameworks

● Post-PHP v. 5.3– Symfony 2+, ZendFramework 2.0, Silex, (Lithium?

CakePHP 2+ ?)

● More modular than predecessors● Compliant with PHP-FIG (PSR-0)● Use modern PHP extensions, testing tools,

OOP, abstraction layers

Page 31: "Building Modern PHP Applications" - Jackson Murtha, South Dakota Code Camp 2012

Symfony 2.0

● Installation/dependency resolution: Composer

Page 32: "Building Modern PHP Applications" - Jackson Murtha, South Dakota Code Camp 2012

Component Development

● Bundles● Scaffolding● ORM● Components

symfony.com/components:– Routing

– EventDispatcher

– HttpFoundation/HttpKernel

– Assetic

Page 33: "Building Modern PHP Applications" - Jackson Murtha, South Dakota Code Camp 2012

Run Symfony

● run php ./app/console server:run● or configure webserver manually

Page 34: "Building Modern PHP Applications" - Jackson Murtha, South Dakota Code Camp 2012

Create a bundle: the “hard” way

● Create a bundle directory in src/– e.g. src/MyPackage/MyBundle

● Create bundle definition class● Create controller class● Create routes● Create view● Register bundle in AppKernel

Page 35: "Building Modern PHP Applications" - Jackson Murtha, South Dakota Code Camp 2012

Create the bundle definition class

● contains namespaces and build functions for the bundle, if necessary

Page 36: "Building Modern PHP Applications" - Jackson Murtha, South Dakota Code Camp 2012

Create a controller class

Page 37: "Building Modern PHP Applications" - Jackson Murtha, South Dakota Code Camp 2012

Create view / Create routes

Page 38: "Building Modern PHP Applications" - Jackson Murtha, South Dakota Code Camp 2012

Register the bundle in AppKernel

Page 39: "Building Modern PHP Applications" - Jackson Murtha, South Dakota Code Camp 2012

Create a bundle: the easy way

● Generates:– template skeleton– resource directories– appKernel registry– unit test skeleton– bundle definition class– default controller

● multiple routing options

Page 40: "Building Modern PHP Applications" - Jackson Murtha, South Dakota Code Camp 2012

Run the app

● Start / visit webserver– app/console server:run

● Navigate to your route to see the response– e.g. http://localhost:8000/app_dev.php/yourRoute/yourParameter

● How is this better than non-framework PHP?● How is this better than existing (first-generation) frameworks?

Page 41: "Building Modern PHP Applications" - Jackson Murtha, South Dakota Code Camp 2012

The End...

● Questions...● Feedback...