+ All Categories
Home > Documents > STANDARDS A PHP DEVELOPER SHOULD...

STANDARDS A PHP DEVELOPER SHOULD...

Date post: 08-Aug-2020
Category:
Upload: others
View: 7 times
Download: 0 times
Share this document with a friend
149
STANDARDS A PHP DEVELOPER SHOULD KNOW @movetodevnull sebastianfeldmann Sebastian Feldmann TEN
Transcript
Page 1: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

STANDARDS A PHP DEVELOPER SHOULD KNOW

@movetodevnullsebastianfeldmann

Sebastian Feldmann

T E N

Page 2: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

STANDARDS ARE

EVERYWHERE

Page 3: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian
Page 4: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian
Page 5: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian
Page 6: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian
Page 7: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian
Page 8: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

The web is build on standards

Page 9: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian
Page 10: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian
Page 11: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian
Page 12: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

💩 encodingUTF8

UTF16

ISO8859-1

ANSI

CP1252

ASCIIEBCDIC273

Page 13: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

w w w . p h p - f i g . o r gPHP-FIG

Page 14: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

•Composer

•Drupal

• Joomla

•Magento

•ReactPHP

• Slim

• Symfony

• Zend Framework

•…

Page 15: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

PSR vs. RFC

Page 16: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

PSR2 PSR12PSR1

Page 17: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

Coding Standards

Page 18: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian
Page 19: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

<?php namespace Vendor\Package;

use FooInterface; use BarClass as Bar; use OtherVendor\OtherPackage\BazClass;

class Foo extends Bar implements FooInterface { public function sampleMethod($a, $b = null) { if ($a === $b) { bar(); } elseif ($a > $b) { $foo->bar($arg1); } else { BazClass::bar($arg2, $arg3); } }

final public static function bar() { // method body } }

Page 20: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

<?php namespace Vendor\Package;

class Foo extends Bar implements FooInterface { public function sampleMethod($a, $b = null) { if ($this->someImportantProperty === $this->someEvenMoreImportantProperty && $this->currentState !== self::SOME_STATE_YOU_DONT_WANT) { throw new \RuntimeException('Something went wrong!'); }

... }

...

Page 21: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

<?php namespace Vendor\Package;

class Foo extends Bar implements FooInterface { public function sampleMethod($a, $b = null) { if ( $this->someImportantProperty === $this->someEvenMoreImportantProperty && $this->currentState !== self::SOME_STATE_YOU_DONT_WANT ) { throw new \RuntimeException('Something went wrong!'); }

... }

...

Page 22: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

<?php namespace Vendor\Package;

class Foo extends Bar implements FooInterface { public function sampleMethod($a, $b = null) { $foo = 'some super string';$bar = 'some other stuff';

... }

...

Page 23: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

<?php namespace Vendor\Package;

class Foo extends Bar implements FooInterface { public function sampleMethod($a, $b = null) { $foo = 'some super string’; $bar = 'some other stuff';

... }

...

Page 24: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

PHP Code Sniffer

PHP Coding Standard Fixer

Page 25: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

PSR4PSR0

Page 26: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

Autoloading

Page 27: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

Namespace

Page 28: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

<NamespaceName>(_<SubNamespaceNames>)*_<ClassName>

PHPUnit_Framework_TestCase

PHPUnit

Framework

TestCase.php

\PHPUnit\Framework\TestCase

\<NamespaceName>(\<SubNamespaceNames>)*\<ClassName>

src PHPUnit=>

PSR0

Page 29: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

<NamespaceName>(_<SubNamespaceNames>)*_<ClassName>

PHPUnit_Framework_TestCase

Framework

TestCase.php

\PHPUnit\Framework\TestCase

\<NamespaceName>(\<SubNamespaceNames>)*\<ClassName>

src

PHPUnit PHPUnit=> Framework

TestCase.php

src PHPUnit=>

PSR4

Page 30: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian
Page 31: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

{

...

"autoload": { "psr-4": { "MyNamespace\\": "src/", }, "psr-0": { "OldLib_Lib1_": "libs/lib1", "SomeLib\\Lib2": "libs/lib2" } }

...

}

composer.json

Page 32: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

PSR3

Page 33: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

Logging

Page 34: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

<?php

namespace Psr\Log;

interface LoggerInterface { public function emergency($message, array $context = array());

public function alert($message, array $context = array()); public function critical($message, array $context = array()); public function error($message, array $context = array());

public function warning($message, array $context = array());

public function notice($message, array $context = array());

public function info($message, array $context = array());

public function debug($message, array $context = array());

public function log($level, $message, array $context = array()); }

Page 35: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

<?php

namespace Psr\Log;

interface LoggerInterface { public function emergency($message, array $context = array());

public function alert($message, array $context = array()); public function critical($message, array $context = array()); public function error($message, array $context = array());

public function warning($message, array $context = array());

public function notice($message, array $context = array());

public function info($message, array $context = array());

public function debug($message, array $context = array());

public function log($level, $message, array $context = array()); }

Page 36: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

<?php

namespace Psr\Log;

interface LoggerInterface { public function emergency($message, array $context = array());

public function alert($message, array $context = array()); public function critical($message, array $context = array()); public function error($message, array $context = array());

public function warning($message, array $context = array());

public function notice($message, array $context = array());

public function info($message, array $context = array());

public function debug($message, array $context = array());

public function log($level, $message, array $context = array()); }

Page 37: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

<?php

namespace Psr\Log;

interface LoggerAwareInterface { public function setLogger(LoggerInterface $logger); }

Page 38: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

<?php

namespace Psr\Log;

class LogLevel { const EMERGENCY = 'emergency'; const ALERT = 'alert'; const CRITICAL = 'critical'; const ERROR = 'error'; const WARNING = 'warning'; const NOTICE = 'notice'; const INFO = 'info'; const DEBUG = 'debug'; }

Page 39: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

<?php declare(strict_types=1);

namespace Monolog;

...

class Logger implements LoggerInterface { ...

}

Page 40: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

PSR16PSR6

Page 41: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

Caching

Page 42: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

<?php

namespace Psr\Cache;

interface CacheItemInterface { public function getKey();

public function get();

public function isHit(); public function set($value); public function expiresAt($expiration); public function expiresAfter($time); }

PSR6

Page 43: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

<?php

namespace Psr\Cache;

interface CacheItemInterface { public function getKey();

public function get();

public function isHit(); public function set($value); public function expiresAt($expiration); public function expiresAfter($time); }

PSR6

Page 44: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

<?php

namespace Psr\Cache;

interface CacheItemInterface { public function getKey();

public function get();

public function isHit(); public function set($value); public function expiresAt($expiration); public function expiresAfter($time); }

PSR6

Page 45: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

<?php

namespace Psr\Cache;

interface CacheItemInterface { public function getKey();

public function get();

public function isHit(); public function set($value); public function expiresAt($expiration); public function expiresAfter($time); }

PSR6

Page 46: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

<?php

namespace Psr\Cache;

interface CacheItemInterface { public function getKey();

public function get();

public function isHit(); public function set($value); public function expiresAt($expiration); public function expiresAfter($time); }

PSR6

Page 47: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

<?php

namespace Psr\Cache;

interface CacheItemPoolInterface { public function getItem($key);

public function getItems(array $keys = array());

public function hasItem($key);

public function clear(); public function deleteItem($key);

public function deleteItems(array $keys);

public function save(CacheItemInterface $item);

public function saveDeferred(CacheItemInterface $item);

public function commit(); }

PSR6

Page 48: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

<?php

namespace Psr\Cache;

interface CacheItemPoolInterface { public function getItem($key);

public function getItems(array $keys = array());

public function hasItem($key);

public function clear(); public function deleteItem($key);

public function deleteItems(array $keys);

public function save(CacheItemInterface $item);

public function saveDeferred(CacheItemInterface $item);

public function commit(); }

PSR6

Page 49: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

<?php

namespace Psr\Cache;

interface CacheItemPoolInterface { public function getItem($key);

public function getItems(array $keys = array());

public function hasItem($key);

public function clear(); public function deleteItem($key);

public function deleteItems(array $keys);

public function save(CacheItemInterface $item);

public function saveDeferred(CacheItemInterface $item);

public function commit(); }

PSR6

Page 50: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

<?php

namespace Psr\Cache;

interface CacheItemPoolInterface { public function getItem($key);

public function getItems(array $keys = array());

public function hasItem($key);

public function clear(); public function deleteItem($key);

public function deleteItems(array $keys);

public function save(CacheItemInterface $item);

public function saveDeferred(CacheItemInterface $item);

public function commit(); }

PSR6

Page 51: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

stop

stop

that’s too much

Page 52: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

<?php

namespace Psr\SimpleCache;

interface CacheInterface { public function get($key, $default = null);

public function set($key, $value, $ttl = null); public function delete($key); public function clear();

public function getMultiple($keys, $default = null); public function setMultiple($values, $ttl = null); public function deleteMultiple($keys);

public function has($key); }

PSR16

Page 53: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

<?php

namespace Psr\SimpleCache;

interface CacheInterface { public function get($key, $default = null);

public function set($key, $value, $ttl = null); public function delete($key); public function clear();

public function getMultiple($keys, $default = null); public function setMultiple($values, $ttl = null); public function deleteMultiple($keys);

public function has($key); }

PSR16

Page 54: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

<?php

namespace Psr\SimpleCache;

interface CacheInterface { public function get($key, $default = null);

public function set($key, $value, $ttl = null); public function delete($key); public function clear();

public function getMultiple($keys, $default = null); public function setMultiple($values, $ttl = null); public function deleteMultiple($keys);

public function has($key); }

PSR16

Page 55: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

PSR11

Page 56: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

Container Interface

aka Container Interop

Page 57: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

<?php

namespace Psr\Container;

interface ContainerInterface { public function get($id);

public function has($id); }

PSR11

Page 58: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

Dependency Injection

Page 59: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

<?php

class Foo { public function __construct() { $handler = new Bar(); $handler->handleInternalStuff($this->value); ... } }

Page 60: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

<?php

class Foo { public function __construct() { $handler = new Bar(); $handler->handleInternalStuff($this->value); ... } }

Page 61: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

<?php

class Foo { public function __construct() { $handler = new Bar(); $handler->handleInternalStuff($this->value); ... } }

Page 62: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

<?php

class Foo { public function __construct() { $handler = new Bar(); $handler->handleInternalStuff($this->value); ... } }

Page 63: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

<?php

class Foo { public function __construct() {

$handler->handleInternalStuff($this->value); ... } }

Page 64: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

<?php

class Foo { public function __construct( ) {

$handler->handleInternalStuff($this->value); ... } }

Page 65: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

<?php

class Foo { public function __construct(Bar $handler) {

$handler->handleInternalStuff($this->value); ... } }

Page 66: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

<?php

class Foo { public function __construct(Bar $handler) { $handler->handleInternalStuff($this->value); ... } }

Page 67: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

Dependency Injection

• loose coupling

• more flexibility

• easier testing

• better code

• complex object creation

+ -

Page 68: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

<?php

$someComplexObject = $container->get('MyComplexObject');

Page 69: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

• Disco

• League/Container

• Pimple

• Zend/ServiceManager

PSR11 - Container

Page 70: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

• Slim

• zend-expressive

PSR11 - Frameworks

Page 71: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

PSR7

Page 72: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

HTTP Message

Page 73: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

Request

$_GET

$_POST

$_FILES

http_get_request_body()

$_COOKIE

http_get_request_headers()

Page 74: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

Response

print()

setcookie()

echo

http_response_code()

header()

http_redirect()

Page 75: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian
Page 76: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

Request

Page 77: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

<?php namespace Psr\Http\Message;

interface MessageInterface { public function getProtocolVersion();

public function withProtocolVersion($version);

public function getHeaders();

public function hasHeader($name);

public function getHeader($name);

public function getHeaderLine($name);

public function withHeader($name, $value);

public function withAddedHeader($name, $value);

public function withoutHeader($name);

public function getBody();

public function withBody(StreamInterface $body); }

Page 78: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

<?php namespace Psr\Http\Message;

interface RequestInterface extends MessageInterface { public function getRequestTarget();

public function withRequestTarget($requestTarget);

public function getMethod();

public function withMethod($method);

public function getUri();

public function withUri(UriInterface $uri, $preserveHost = false); }

Page 79: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

<?php namespace Psr\Http\Message;

interface ServerRequestInterface extends RequestInterface { public function getServerParams();

public function getCookieParams();

public function withCookieParams(array $cookies);

public function getQueryParams();

public function withQueryParams(array $query);

public function getUploadedFiles();

public function withUploadedFiles(array $uploadedFiles);

public function getParsedBody();

public function withParsedBody($data);

public function getAttributes();

public function getAttribute($name, $default = null);

public function withAttribute($name, $value);

public function withoutAttribute($name); }

Page 80: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

<?php namespace Psr\Http\Message;

interface MessageInterface { public function getProtocolVersion();

public function withProtocolVersion($version);

public function getHeaders();

public function hasHeader($name);

public function getHeader($name);

public function getHeaderLine($name);

public function withHeader($name, $value);

public function withAddedHeader($name, $value);

public function withoutHeader($name);

public function getBody();

public function withBody(StreamInterface $body); }

Page 81: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

<?php namespace Psr\Http\Message;

interface MessageInterface { public function getProtocolVersion();

public function withProtocolVersion($version);

public function getHeaders();

public function hasHeader($name);

public function getHeader($name);

public function getHeaderLine($name);

public function withHeader($name, $value);

public function withAddedHeader($name, $value);

public function withoutHeader($name);

public function getBody();

public function withBody(StreamInterface $body); }

Page 82: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

<?php namespace Psr\Http\Message;

interface MessageInterface { public function getProtocolVersion();

public function withProtocolVersion($version);

public function getHeaders();

public function hasHeader($name);

public function getHeader($name);

public function getHeaderLine($name);

public function withHeader($name, $value);

public function withAddedHeader($name, $value);

public function withoutHeader($name);

public function getBody();

public function withBody(StreamInterface $body); }

Page 83: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

<?php namespace Psr\Http\Message;

interface RequestInterface extends MessageInterface { public function getRequestTarget();

public function withRequestTarget($requestTarget);

public function getMethod();

public function withMethod($method);

public function getUri();

public function withUri(UriInterface $uri, $preserveHost = false); }

Page 84: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

<?php namespace Psr\Http\Message;

interface RequestInterface extends MessageInterface { public function getRequestTarget();

public function withRequestTarget($requestTarget);

public function getMethod();

public function withMethod($method);

public function getUri();

public function withUri(UriInterface $uri, $preserveHost = false); }

Page 85: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

<?php namespace Psr\Http\Message;

interface ServerRequestInterface extends RequestInterface { public function getServerParams();

public function getCookieParams();

public function withCookieParams(array $cookies);

public function getQueryParams();

public function withQueryParams(array $query);

public function getUploadedFiles();

public function withUploadedFiles(array $uploadedFiles);

public function getParsedBody();

public function withParsedBody($data);

public function getAttributes();

public function getAttribute($name, $default = null);

public function withAttribute($name, $value);

public function withoutAttribute($name); }

Page 86: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

<?php namespace Psr\Http\Message;

interface ServerRequestInterface extends RequestInterface { public function getServerParams();

public function getCookieParams();

public function withCookieParams(array $cookies);

public function getQueryParams();

public function withQueryParams(array $query);

public function getUploadedFiles();

public function withUploadedFiles(array $uploadedFiles);

public function getParsedBody();

public function withParsedBody($data);

public function getAttributes();

public function getAttribute($name, $default = null);

public function withAttribute($name, $value);

public function withoutAttribute($name); }

Page 87: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

<?php namespace Psr\Http\Message;

interface ServerRequestInterface extends RequestInterface { public function getServerParams();

public function getCookieParams();

public function withCookieParams(array $cookies);

public function getQueryParams();

public function withQueryParams(array $query);

public function getUploadedFiles();

public function withUploadedFiles(array $uploadedFiles);

public function getParsedBody();

public function withParsedBody($data);

public function getAttributes();

public function getAttribute($name, $default = null);

public function withAttribute($name, $value);

public function withoutAttribute($name); }

Page 88: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

<?php namespace Psr\Http\Message;

interface ServerRequestInterface extends RequestInterface { public function getServerParams();

public function getCookieParams();

public function withCookieParams(array $cookies);

public function getQueryParams();

public function withQueryParams(array $query);

public function getUploadedFiles();

public function withUploadedFiles(array $uploadedFiles);

public function getParsedBody();

public function withParsedBody($data);

public function getAttributes();

public function getAttribute($name, $default = null);

public function withAttribute($name, $value);

public function withoutAttribute($name); }

Page 89: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

<?php namespace Psr\Http\Message;

interface ServerRequestInterface extends RequestInterface { public function getServerParams();

public function getCookieParams();

public function withCookieParams(array $cookies);

public function getQueryParams();

public function withQueryParams(array $query);

public function getUploadedFiles();

public function withUploadedFiles(array $uploadedFiles);

public function getParsedBody();

public function withParsedBody($data);

public function getAttributes();

public function getAttribute($name, $default = null);

public function withAttribute($name, $value);

public function withoutAttribute($name); }

Page 90: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

<?php namespace Psr\Http\Message;

interface ServerRequestInterface extends RequestInterface { public function getServerParams();

public function getCookieParams();

public function withCookieParams(array $cookies);

public function getQueryParams();

public function withQueryParams(array $query);

public function getUploadedFiles();

public function withUploadedFiles(array $uploadedFiles);

public function getParsedBody();

public function withParsedBody($data);

public function getAttributes();

public function getAttribute($name, $default = null);

public function withAttribute($name, $value);

public function withoutAttribute($name); }

Page 91: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

[ 'files' => [ 'name' => [ 0 => 'file0.txt', 1 => 'file1.html', ], 'type' => [ 0 => 'text/plain', 1 => 'text/html', ],

...

], ]

Page 92: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

[ 'files' => [ 0 => [ 'name' => 'file0.txt', 'type' => ‚text/plain',

...

], 1 => [ 'name' => 'file1.html', 'type' => ‚text/html',

...

], ], ];

Page 93: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

[ 'files' => [

0 => /* UploadedFileInterface instance */

1 => /* UploadedFileInterface instance */

] ];

Page 94: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

<?php namespace Psr\Http\Message;

interface UploadedFileInterface { public function getStream();

public function moveTo($targetPath);

public function getSize();

public function getError();

public function getClientFilename();

public function getClientMediaType(); }

Page 95: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

<?php namespace Psr\Http\Message;

interface UploadedFileInterface { public function getStream();

public function moveTo($targetPath);

public function getSize();

public function getError();

public function getClientFilename();

public function getClientMediaType(); }

Page 96: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

<?php namespace Psr\Http\Message;

interface UploadedFileInterface { public function getStream();

public function moveTo($targetPath);

public function getSize();

public function getError();

public function getClientFilename();

public function getClientMediaType(); }

Page 97: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

<?php namespace Psr\Http\Message;

interface UploadedFileInterface { public function getStream();

public function moveTo($targetPath);

public function getSize();

public function getError();

public function getClientFilename();

public function getClientMediaType(); }

Page 98: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

<?php namespace Psr\Http\Message;

interface UploadedFileInterface { public function getStream();

public function moveTo($targetPath);

public function getSize();

public function getError();

public function getClientFilename();

public function getClientMediaType(); }

Page 99: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

<?php namespace Psr\Http\Message;

interface UriInterface { public function getScheme();

public function getAuthority();

public function getUserInfo();

public function getHost();

public function getPort();

public function getPath();

public function getQuery();

public function getFragment();

public function withScheme($scheme);

public function withUserInfo($user, $password = null);

public function withHost($host);

public function withPort($port);

public function withPath($path);

public function withQuery($query);

public function withFragment($fragment);

public function __toString(); }

Page 100: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

<?php namespace Psr\Http\Message;

interface UriInterface { public function getScheme();

public function getAuthority();

public function getUserInfo();

public function getHost();

public function getPort();

public function getPath();

public function getQuery();

public function getFragment();

public function withScheme($scheme);

public function withUserInfo($user, $password = null);

public function withHost($host);

public function withPort($port);

public function withPath($path);

public function withQuery($query);

public function withFragment($fragment);

public function __toString(); }

Page 101: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

<?php namespace Psr\Http\Message;

interface UriInterface { public function getScheme();

public function getAuthority();

public function getUserInfo();

public function getHost();

public function getPort();

public function getPath();

public function getQuery();

public function getFragment();

public function withScheme($scheme);

public function withUserInfo($user, $password = null);

public function withHost($host);

public function withPort($port);

public function withPath($path);

public function withQuery($query);

public function withFragment($fragment);

public function __toString(); }

Page 102: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

<?php namespace Psr\Http\Message;

interface UriInterface { public function getScheme();

public function getAuthority();

public function getUserInfo();

public function getHost();

public function getPort();

public function getPath();

public function getQuery();

public function getFragment();

public function withScheme($scheme);

public function withUserInfo($user, $password = null);

public function withHost($host);

public function withPort($port);

public function withPath($path);

public function withQuery($query);

public function withFragment($fragment);

public function __toString(); }

Page 103: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

<?php namespace Psr\Http\Message;

interface UriInterface { public function getScheme();

public function getAuthority();

public function getUserInfo();

public function getHost();

public function getPort();

public function getPath();

public function getQuery();

public function getFragment();

public function withScheme($scheme);

public function withUserInfo($user, $password = null);

public function withHost($host);

public function withPort($port);

public function withPath($path);

public function withQuery($query);

public function withFragment($fragment);

public function __toString(); }

Page 104: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

<?php namespace Psr\Http\Message;

interface UriInterface { public function getScheme();

public function getAuthority();

public function getUserInfo();

public function getHost();

public function getPort();

public function getPath();

public function getQuery();

public function getFragment();

public function withScheme($scheme);

public function withUserInfo($user, $password = null);

public function withHost($host);

public function withPort($port);

public function withPath($path);

public function withQuery($query);

public function withFragment($fragment);

public function __toString(); }

Page 105: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

Request

• Query data

• POST data

• Cookie data

• Uploaded files

• URI information

• Stream data

Page 106: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

Response

Page 107: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

<?php namespace Psr\Http\Message;

interface MessageInterface { public function getProtocolVersion();

public function withProtocolVersion($version);

public function getHeaders();

public function hasHeader($name);

public function getHeader($name);

public function getHeaderLine($name);

public function withHeader($name, $value);

public function withAddedHeader($name, $value);

public function withoutHeader($name);

public function getBody();

public function withBody(StreamInterface $body); }

Page 108: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

<?php namespace Psr\Http\Message;

interface MessageInterface { public function getProtocolVersion();

public function withProtocolVersion($version);

public function getHeaders();

public function hasHeader($name);

public function getHeader($name);

public function getHeaderLine($name);

public function withHeader($name, $value);

public function withAddedHeader($name, $value);

public function withoutHeader($name);

public function getBody();

public function withBody(StreamInterface $body); }

Page 109: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

<?php namespace Psr\Http\Message;

interface ResponseInterface extends MessageInterface { public function getStatusCode();

public function withStatus($code, $reasonPhrase = '');

public function getReasonPhrase(); }

Page 110: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

<?php namespace Psr\Http\Message;

interface ResponseInterface extends MessageInterface { public function getStatusCode();

public function withStatus($code, $reasonPhrase = '');

public function getReasonPhrase(); }

Page 111: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

<?php namespace Psr\Http\Message;

interface ResponseInterface extends MessageInterface { public function getStatusCode();

public function withStatus($code, $reasonPhrase = '');

public function getReasonPhrase(); }

Page 112: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

PSR7

Request

Response

Request

Request

Response

Page 113: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

• Slim

• zend-expressive

• Laravel

• Symfony (PSR-7-Bridge)

PSR7 - Frameworks

Page 114: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

PSR17 PSR18

Page 115: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

PSR17HTTP Factory

Page 116: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

<?php

namespace Psr\Http\Message;

Page 117: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

<?php

namespace Psr\Http\Message;

interface RequestFactoryInterface

Page 118: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

<?php

namespace Psr\Http\Message;

interface RequestFactoryInterface

interface ServerRequestFactoryInterface

Page 119: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

<?php

namespace Psr\Http\Message;

interface RequestFactoryInterface

interface ServerRequestFactoryInterface

interface ResponseFactoryInterface

Page 120: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

<?php

namespace Psr\Http\Message;

interface RequestFactoryInterface

interface ServerRequestFactoryInterface

interface ResponseFactoryInterface

interface UploadedFileFactoryInterface

Page 121: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

<?php

namespace Psr\Http\Message;

interface RequestFactoryInterface

interface ServerRequestFactoryInterface

interface ResponseFactoryInterface

interface UploadedFileFactoryInterface

interface UriFactoryInterface

Page 122: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

<?php

namespace Psr\Http\Message;

interface RequestFactoryInterface

interface ServerRequestFactoryInterface

interface ResponseFactoryInterface

interface UploadedFileFactoryInterface

interface UriFactoryInterface

interface StreamFactoryInterface

Page 123: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

PSR18HTTP Client

Page 124: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

<?php

namespace Psr\Http\Client;

use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface;

interface ClientInterface { public function sendRequest(RequestInterface $request): ResponseInterface; }

Page 125: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

<?php

namespace Psr\Http\Client;

use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface;

interface ClientInterface { public function sendRequest(RequestInterface $request): ResponseInterface; }

Page 126: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

Guzzle

Page 127: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

PSR15HTTP Handler

Page 128: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

<?php

namespace Psr\Http\Server;

use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface;

interface RequestHandlerInterface { public function handle(ServerRequestInterface $request): ResponseInterface; }

Page 129: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

<?php

namespace Psr\Http\Server;

use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface;

interface MiddlewareInterface { public function process( ServerRequestInterface $request, RequestHandlerInterface $handler ): ResponseInterface; }

Page 130: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian
Page 131: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

Middleware examples

• Authentication

• Authorization

• Redirecting / Social Routing

• Output Formatting

Page 132: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

<?php

use Psr\Http\Server\MiddlewareInterface; use Psr\Http\Server\RequestHandlerInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface;

class FinalResponder implements MiddlewareInterface { public function process( ServerRequestInterface $request, RequestHandlerInterface $handler ): ResponseInterface { try { return $handler->handle($request); } catch (\Exception $e) { return ErrorPage::createResponse($e); } } }

Page 133: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

<?php

use Psr\Http\Server\MiddlewareInterface; use Psr\Http\Server\RequestHandlerInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface;

class FinalResponder implements MiddlewareInterface { public function process( ServerRequestInterface $request, RequestHandlerInterface $handler ): ResponseInterface { try { return $handler->handle($request); } catch (\Exception $e) { return ErrorPage::createResponse($e); } } }

Page 134: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

<?php

use Psr\Http\Server\MiddlewareInterface; use Psr\Http\Server\RequestHandlerInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface;

class FinalResponder implements MiddlewareInterface { public function process( ServerRequestInterface $request, RequestHandlerInterface $handler ): ResponseInterface { try { return $handler->handle($request); } catch (\Exception $e) { return ErrorPage::createResponse($e); } } }

Page 135: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

<?php

use Psr\Http\Server\MiddlewareInterface; use Psr\Http\Server\RequestHandlerInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface;

class FinalResponder implements MiddlewareInterface { public function process( ServerRequestInterface $request, RequestHandlerInterface $handler ): ResponseInterface { try { return $handler->handle($request); } catch (\Exception $e) { return ErrorPage::createResponse($e); } } }

Page 136: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

<?php

use Psr\Http\Server\MiddlewareInterface; use Psr\Http\Server\RequestHandlerInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface;

class FinalResponder implements MiddlewareInterface { public function process( ServerRequestInterface $request, RequestHandlerInterface $handler ): ResponseInterface { try { return $handler->handle($request); } catch (\Exception $e) { return ErrorPage::createResponse($e); } } }

Page 137: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

• Single Responsibility

• Tiny testable peaces

• Defined data flow

PSR15 - Middleware

Page 138: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

PSR14

Page 139: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

Event ManagerDRAFT

Page 140: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

• Events

• Tasks

• Emitter

• EventLister

• Processor

PSR14

Page 141: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

• Security framework that handles access permissions

• Common full page caching system

• Logging package to track all actions

PSR14

Page 142: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

PSR8

Page 143: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

Huggable Interface

Page 144: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

<?php namespace Psr\Hug;

/** * Defines a huggable object. * * A huggable object expresses mutual affection with another huggable object. */ interface Huggable {

/** * Hugs this object. * * All hugs are mutual. An object that is hugged MUST in turn hug the other * object back by calling hug() on the first parameter. All objects MUST * implement a mechanism to prevent an infinite loop of hugging. * * @param Huggable $h * The object that is hugging this object. */ public function hug(Huggable $h); }

Page 145: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian
Page 146: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

💩

PSR8

PSR15 PSR7

PSR18

PSR2

PSR6

PSR8PSR12

PSR4

PSR11

Page 147: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

Deadpool loves standards

Page 148: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian
Page 149: STANDARDS A PHP DEVELOPER SHOULD KNOWsebastian-feldmann.info/talks/2018/20181016-10-standards-a-php... · STANDARDS A PHP DEVELOPER SHOULD KNOW sebastianfeldmann @movetodevnull Sebastian

Sebastian Feldmann

@movetodevnull

https://phpbu.de

sebastianfeldmann

Captain Hook


Recommended