Share
Embed
Transcript
131 slides
-
szymanskilukasz Łukasz Szymański Development Team Lead at @szymanskilukasz https://www.linkedin.com/in/szymanskilukasz https://twitter.com/szymanskilukasz
https://speakerdeck.com/szymanskilukasz -
szymanskilukasz // PHP 5 era code that will break. function
handler(Exception $e) { ... } set_exception_handler('handler'); // PHP 5 and 7 compatible. function handler($e) { ... } // PHP 7 only. function handler(Throwable $e) { ... } -
szymanskilukasz memory object serialized by memcache serialization / deserialization application
memcache memcached serialize deserialize x -
szymanskilukasz APC is opcode cache and data store APCu is
only data store -
szymanskilukasz APC is opcode cache and data store APCu is
only data store OPcache is only opcode cache -
szymanskilukasz APCu is APC stripped of opcode caching. APC is
opcode cache and data store APCu is only data store OPcache is only opcode cache -
szymanskilukasz Reduce Number Of Allocations PHP5 spends 20% of CPU
time in allocator -
szymanskilukasz CPU L1D L2 L3 RAM 32KB 256KB few MB
1ns 4ns 12ns 100ns -
szymanskilukasz class PHPisNotDead { public function addIntegers($a, $b) { return
$a + $b; } public function addFloats($a, $b) { return $a + $b; } public function reverseString($a) { return strrev($a); } public function isItTrue($a) { return $a ? 'yes' : 'no'; } } PHP5 -
szymanskilukasz class PHPisNotDead { public function addIntegers(MyInteger $a, MyInteger $b)
{ return $a + $b; } public function addFloats(MyFloat $a, MyFloat $b) { return $a + $b; } public function reverseString(MyString $a) { return strrev($a); } public function isItTrue($a) { return $a ? 'yes' : 'no'; } } PHP5 + Value Objects -
szymanskilukasz PHP7 class PHPisNotDead { public function addIntegers(int $a, int
$b) : int { return $a + $b; } public function addFloats(float $a, float $b) : float { return $a + $b; } public function reverseString(string $a) : string { return strrev($a); } public function isItTrue(bool $a) : string { return $a ? 'yes' : 'no'; } } -
szymanskilukasz PHP7 class PHPisNotDead { public function addIntegers(int $a, int
$b) : int { return $a + $b; } public function addFloats(float $a, float $b) : float { return $a + $b; } public function reverseString(string $a) : string { return strrev($a); } public function isItTrue(bool $a) : string { return $a ? 'yes' : 'no'; } } -
szymanskilukasz PHP7 $php = new PHPisNotDead(); var_dump($php->addIntegers(1.3, 5.7)); //
int(6) var_dump($php->addFloats(4, '3.4')); // double(7.4) var_dump($php->reverseString(12345)); // string(5) "54321" var_dump($php->isItTrue(21.37)); // string(3) "yes" -
szymanskilukasz PHP7 $php = new PHPisNotDead(); var_dump($php->addIntegers(1.3, 5.7)); //
int(6) var_dump($php->addFloats(4, '3.4')); // double(7.4) var_dump($php->reverseString(12345)); // string(5) "54321" var_dump($php->isItTrue(21.37)); // string(3) "yes" WTF? -
szymanskilukasz PHP7 $php = new PHPisNotDead(); var_dump($php->addIntegers(1.3, 5.7)); //
int(6) var_dump($php->addFloats(4, '3.4')); // double(7.4) var_dump($php->reverseString(12345)); // string(5) "54321" var_dump($php->isItTrue(21.37)); // string(3) "yes" Coercive Mode (default) -
szymanskilukasz public function addIntegers(int $a,int $b) : int { return
$a + $b; } public function addIntegers($a, $b) { $a = (int) $a; $b = (int) $b; return (int) ($a + $b); } PHP7 -
szymanskilukasz public function addIntegers(int $a,int $b) : int { return
$a + $b; } public function addIntegers($a, $b) { $a = (int) $a; $b = (int) $b; return (int) ($a + $b); } PHP7 -
szymanskilukasz declare (strict_types=1); class PHPisNotDead { public function addIntegers(int $a,
int $b) : int { return $a + $b; } public function addFloats(float $a, float $b) : float { return $a + $b; } public function reverseString(string $a) : string { return strrev($a); } public function isItTrue(bool $a) : string { return $a ? 'yes' : 'no'; } } PHPisNotDead.php -
szymanskilukasz index.php require "PHPisNotDead.php"; $php = new PHPisNotDead(); var_dump($php->addIntegers(1.3,
5.7)); // int(6) var_dump($php->addFloats(4, '3.4')); // double(7.4) var_dump($php->reverseString(12345)); // string(5) "54321" var_dump($php->isItTrue(21.37)); // string(3) "yes" -
szymanskilukasz index.php require "PHPisNotDead.php"; $php = new PHPisNotDead(); var_dump($php->addIntegers(1.3,
5.7)); // int(6) var_dump($php->addFloats(4, '3.4')); // double(7.4) var_dump($php->reverseString(12345)); // string(5) "54321" var_dump($php->isItTrue(21.37)); // string(3) "yes" WTF2? -
szymanskilukasz declare (strict_types=1); class PHPisNotDead { public function addIntegers(int $a,
int $b) : int { return $a + $b; } public function addFloats(float $a, float $b) : float { return $a + $b; } public function reverseString(string $a) : string { return strrev($a); } public function isItTrue(bool $a) : string { return $a ? 'yes' : 'no'; } } PHPisNotDead.php -
szymanskilukasz declare (strict_types=1); class PHPisNotDead { public function addIntegers(int $a,
int $b) : int { return $a + $b; } public function addFloats(float $a, float $b) : float { return $a + $b; } public function reverseString(string $a) : string { return strrev($a); } public function isItTrue(bool $a) : string { return $a ? 'yes' : 'no'; } } PHPisNotDead.php -
szymanskilukasz PHPisNotDead.php declare (strict_types = 1); class PHPisNotDead {
public function addIntegers(int $a, int $b) : int { return (string) ($a + $b); } public function addFloats(float $a, float $b) : float { return $a + $b; } public function reverseString(string $a) : string { return strrev($a); } public function isItTrue(bool $a) : string { return $a ? 'yes' : 'no'; } } -
szymanskilukasz Fatal error: Uncaught TypeError: Return value of PHPisNotDead::addIntegers() must
be of the type integer, string returned -
szymanskilukasz declare (strict_types = 1); require "PHPisNotDead.php"; $php
= new PHPisNotDead(); var_dump($php->addIntegers(1.3, 5.7)); var_dump($php->addFloats(4, '3.4')); var_dump($php->reverseString(12345)); var_dump($php->isItTrue(21.37)); -
szymanskilukasz declare (strict_types = 1); require "PHPisNotDead.php"; $php
= new PHPisNotDead(); var_dump($php->addIntegers(1.3, 5.7)); var_dump($php->addFloats(4, '3.4')); var_dump($php->reverseString(12345)); var_dump($php->isItTrue(21.37)); -
szymanskilukasz TypeError: Argument 1 passed to PHPisNotDead::addIntegers() must be of
the type integer, float given -
szymanskilukasz $php = new PHPisNotDead(); try { var_dump($php->addIntegers(1.3, 5.7)); }
catch (TypeError $error) { echo $error->getMessage(); } catch (Error $error) { echo $error->getMessage(); } catch (Exception $exception) { echo $exception->getMessage(); } catch (Throwable $throwable) { echo $throwable->getMessage(); } -
szymanskilukasz function should_return_nothing(): void { return 1; // Fatal error:
A void function must not return a value } function lacks_return(): void { // valid } function returns_nothing(): void { return; // valid } function returns_null(): void { return null; // Fatal error: A void function must not return a value } function foobar(void $foo) {} // Fatal error: void cannot be used as a parameter type -
szymanskilukasz checked at compile-time error is produced without the function
needing to be called. -
szymanskilukasz class Token { // Constants default to public const
PUBLIC_CONST = 0; // Constants then also can have a defined visibility private const PRIVATE_CONST = 0; protected const PROTECTED_CONST = 0; public const PUBLIC_CONST_TWO = 0; //Constants can only have one visibility declaration list private const FOO = 1, BAR = 2; } -
szymanskilukasz //Interfaces only support public consts, // and a compile
time error will be thrown // for anything else. // Mirroring the behavior of methods. interface ICache { public const PUBLIC = 0; const IMPLICIT_PUBLIC = 1; } -
szymanskilukasz function answer(): ?int { return null; //ok }
function answer(): ?int { return 42; // ok } function answer(): ?int { return new stdclass(); // error } -
szymanskilukasz function say(?string $msg) { if ($msg) { echo $msg;
} } say('hello'); // ok -- prints hello say(null); // ok -- does not print say(); // error -- missing parameter say(new stdclass); //error -- bad type -
szymanskilukasz // nullability can be removed by a subclass interface
Fooable { function foo(): ?Fooable; } interface StrictFooable extends Fooable { function foo(): Fooable; // valid } -
szymanskilukasz // but it cannot be added interface Fooable {
function foo(): Fooable; } interface LooseFooable extends Fooable { function foo(): ?Fooable; // invalid } -
szymanskilukasz // Valid use: loosening the nullable marker // in
a parameter: interface Fooable { function foo(Fooable $f); } interface LooseFoo extends Fooable { function foo(?Fooable $f); } -
szymanskilukasz // Invalid use: tightening // the nullable marker in
a parameter: interface Fooable { function foo(?Fooable $f); } interface StrictFoo extends Fooable { function foo(Fooable $f); } -
szymanskilukasz // If a value is not an array or
instance of Traversable, // a TypeError will be thrown. function foo(iterable $iterable) { foreach ($iterable as $value) { // ... } } // If the returned value is not an array // or instance of Traversable, a TypeError will be thrown. function bar(): iterable { return [1, 2, 3]; } // Parameters declared as iterable // may use null or an array as a default value function foo(iterable $iterable = []) { // ... } -
szymanskilukasz class Person { friend HumanResourceReport; protected $id; protected
$firstName; protected $lastName; public function __construct($id, $firstName, $lastName) { $this->id = $id; $this->firstName = $firstName; $this->lastName = $lastName; } public function makeReport() { return new HumanResourceReport($this); } } -
szymanskilukasz class HumanResourceReport { private $person; public function __construct(Person
$person) { $this->person = $person; } public function getFullName() { // HumanResourceReport would not have access to protected // members of Person if not explicitly listed as a friend. return $this->person->firstName . ' ' . $this->person->lastName; } public function getReportIdentifier() { return "HR_REPORT_ID_{$this->person->id}"; } } -
szymanskilukasz class HumanResourceReport { private $person; public function __construct(Person
$person) { $this->person = $person; } public function getFullName() { // HumanResourceReport would not have access to protected // members of Person if not explicitly listed as a friend. return $this->person->firstName . ' ' . $this->person->lastName; } public function getReportIdentifier() { return "HR_REPORT_ID_{$this->person->id}"; } } -
szymanskilukasz $person = new Person(Uuid::uuid4(), 'Alice', 'Wonderland'); $report = $person->makeReport();
var_dump($report->getFullName()); // string(16) "Alice Wonderland" var_dump($report->getReportIdentifier()); // string(49) "HR_REPORT_ID_3F2504E0-4F89-41D3-9A0C-0305E82C3301" -
szymanskilukasz // Using positional arguments: array_fill(0, 100, 42); // Using
named arguments: array_fill(start_index => 0, num => 100, value => 42); -
szymanskilukasz htmlspecialchars($string, default, default, false); // vs htmlspecialchars($string, double_encode =>
false); $str->contains("foo", true); // vs $str->contains("foo", caseInsensitive => true); -
szymanskilukasz class Entry<KeyType, ValueType> { protected $key; protected $value;
public function __construct(KeyType $key, ValueType $value) { $this->key = $key; $this->value = $value; } public function getKey(): KeyType { return $this->key; } public function getValue(): ValueType { return $this->value; } } -
szymanskilukasz class Entry<KeyType, ValueType> { protected $key; protected $value;
public function __construct(KeyType $key, ValueType $value) { $this->key = $key; $this->value = $value; } public function getKey(): KeyType { return $this->key; } public function getValue(): ValueType { return $this->value; } } -
szymanskilukasz $entry = new Entry<int,string>(1, 'test'); $entry = new
Entry(1, 'test'); -
szymanskilukasz class Email { private $_email; public function __construct
($email) { // validation $this->_email = $email; } public function getValue() { return $this->_email; } } -
szymanskilukasz immutable class Email { public $email; public function
__construct ($email) { // validation $this->email = $email; } } -
szymanskilukasz immutable class Email { public $email; public function
__construct ($email) { // validation $this->email = $email; } } -
szymanskilukasz immutable class Email { public $email; public function
__construct ($email) { // validation $this->email = $email; } } -
szymanskilukasz immutable class Email { public $email; public function
__construct ($email) { // validation $this->email = $email; } } $email = new Email("[email protected]"); $email->email = "[email protected]" // Call will result in Fatal Error -
szymanskilukasz class Email { public immutable $email; public function
__construct ($email) { // validation $this->email = $email; } } $email = new Email("[email protected]"); $email->email = "[email protected]" // Call will result in Fatal Error -
szymanskilukasz class BoxedInt { public int $value; } $i
= new BoxedInt(); $i->value = 'oops'; // Catchable fatal error: // property BoxedInt::$value must be of // the type integer, string given -
szymanskilukasz Łukasz Szymański Development Team Lead at @szymanskilukasz https://www.linkedin.com/in/szymanskilukasz https://twitter.com/szymanskilukasz
https://speakerdeck.com/szymanskilukasz