27
Modern . Refresh Cambridge, 2 Sept 2013 Simon R Jones, Studio 24 www.studio24.net

Modern PHP

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Modern PHP

Modern .

Refresh Cambridge, 2 Sept 2013

Simon R Jones, Studio 24www.studio24.net

Page 2: Modern PHP

So.. PHP

• Has a bad rep, it’s cool to diss PHP

• Massively successful language

• Low barrier to entry

• Bad practices

Page 3: Modern PHP

PHP..

• Naming inconsistencies

• register_globals

• Unpredictable releases. 5.2 in 2006, 5.3 in 2009, PHP 6?..

• However...It’s a great language for getting things done

Page 5: Modern PHP

PHP internal dev process

• Predictable yearly release cycle

• Support old versions for 3 years

• Development on GitHub - github.com/php/php-src

• Public RFCs - wiki.php.net/rfc

Page 6: Modern PHP

PHP 5.3(pretty old now)

• Released June 2009

• Namespaces

• Closures / Anonymous functions

• DateTime manipulation

Page 7: Modern PHP

Namespaces

// My/Class/Example.php namespace My\Class;

// In the past was called My_Class_Exampleclass Example {

}

Page 8: Modern PHP

Namespacesnamespace My\Class;use Zend\Http\Client;

class Example {

// My\Class\Foo $fish = new Foo()

$client = new Client(); $client->setUri('http://bbc.co.uk'); $response = $client->dispatch();}

http://php.net/language.namespaces

Page 9: Modern PHP

Anonymous functions

// PHP 5.4 array syntax$items = ['lancashire', 'cheddar', 'manchego', 'brie'];

array_walk($items, function(&$value) { if ($value == 'manchego') { $value .= ' is great!'; } $value = ucfirst($value);});

php.net/functions.anonymous

Page 10: Modern PHP

PHP 5.4

• Released March 2012

• Traits

• Short syntax for arrays

• Built-in webserver for testing

• Removed really old stuff: safe_mode, register_globals, magic_quotes

Page 11: Modern PHP

Traits

class Cart { use Maths;}

class Maths { function calculateVAT() { }}

$cart = new Cart()$cart->calculateVAT();

http://php.net/traits

Page 12: Modern PHP

Built-in webserver

$ php -S localhost:8000

PHP 5.4.10 Development Server started at Tue Aug 27 22:47:36 2013Listening on http://localhost:8000Document root is /Users/sjonesPress Ctrl-C to quit.

[Tue Aug 27 22:48:39 2013] ::1:49251 [200]: /

php.net/features.commandline.webserver

Page 13: Modern PHP

PHP 5.5

• Released June 2013

• Simple password hashing!

• Generators

• “finally” for try/catch statements

• Built-in opcode cache

Page 14: Modern PHP

Password hashing

// Create hash$hash = password_hash('refresh');

// Test password$result = password_verify('refresh', $hash);

Forward-compatible PHP 5.3 port -github.com/ircmaxell/password_compat

php.net/password

Page 15: Modern PHP

Community

Page 16: Modern PHP

PHP FIG(Framework Interop Group)

• Common patterns in PHP projects to help code sharing

• PSR-0 standard file, class and namespace conventions to support code sharing

• PSR-1 & PSR-2 coding standards

• PSR-3 common interface for logging

Page 17: Modern PHP

PHP FIG

• Used by projects such as Drupal, Zend, Symfony, CakePHP, phpBB, AWS SDK

• www.php-fig.org

• Nice post on recent updates - philsturgeon.co.uk/blog/2013/08/progress-in-the-phpfig

Page 18: Modern PHP

Composer

• Dependency manager

• Used to install PHP packages

• Loads everything into vendor/

• Autoloader supplied in vendor/autoload.php

• Really, really easy!

Page 19: Modern PHP

Composer

{ "name": "My Project", "require": { "php": ">=5.3.3", "zendframework/zendframework": "2.2.*", "zf-commons/zfc-base": "0.1.*", "zf-commons/zfc-twig": "1.1.*", "phpunit/phpunit": "3.7.*", "zendframework/zend-developer-tools": "0.0.*" }}

Page 20: Modern PHP

$ composer installLoading composer repositories with package informationInstalling dependencies (including require-dev) - Installing zendframework/zendframework (2.2.4) Loading from cache

- Installing zf-commons/zfc-base (v0.1.2)

- Installing twig/twig (v1.13.2) Downloading: 100%

- Installing zf-commons/zfc-twig (1.1.5) Downloading: 100%

- Installing symfony/yaml (v2.3.4)

getcomposer.org

Page 22: Modern PHP

Education

Page 27: Modern PHP

Thanks!

Slides on slideshare.net/simonrjones

Next Refresh meetup first week of NovemberSign up to our mailing list at refreshcambridge.org

Sponsored by Studio 24 - www.studio24.net

@simonrjones@refreshcambs