Hello!
I am Jeremy Lindblom
Longtime PHP lover and co-organizer of azPHP
You can find me at @jeremeamia
2
Slide 3
Slide 3 text
“
I did not develop the PHP we
know today. Dozens, if not
hundreds of people, developed
PHP. I was simply the first
developer.
— R A S M U S L E R D O R F —
3
Slide 4
Slide 4 text
Thank You
Nikita Popov
nikic is an exceptional
programmer, and has
been responsible for so
many of PHP’s recent
improvements, both in
performance & syntax.
4
Slide 5
Slide 5 text
Let’s Review
Let’s start by remembering features
that were added in PHP 7.2 and 7.3
5
What’s New?
Let’s take a look at some of the new
features coming next month in PHP 7.4
7
Slide 8
Slide 8 text
1.
Typed Properties
Can we type everything now? I think so!
Slide 9
Slide 9 text
Typed Properties
class Model {
private array $data;
private bool $loaded;
private string $name;
}
9
Slide 10
Slide 10 text
What Types Can I Use?
array
int
string
bool
float
object
10
iterable
?
callable
void
Slide 11
Slide 11 text
2.
Improved Covariance
and Contravariance
Yay! Factories with generic return types!
Slide 12
Slide 12 text
Limited Return Type Covariance
interface Client {...}
class MyClient implements Client {...}
abstract class ClientFactory {
abstract protected function create(): Client;
}
class MyClientFactory extends ClientFactory {
protected function create(): MyClient {...}
}
12
Slide 13
Slide 13 text
Contravariance in Parameters
class Mapper {
public function map(Iterator $i) {...}
}
class ArrayMapper extends Mapper {
public function map(iterable $iter) {
return parent::map(iterator_for($iter));
}
}
13
Slide 14
Slide 14 text
3.
Preloading
Preloading frameworks into opcache? Nice!
Slide 15
Slide 15 text
Preloading
◦ Part of Opcache
◦ Write a PHP script to preload files
◦ Executed once on server startup
◦ All preloaded files are available in memory
for ALL requests
◦ Changes made to the source file won't have
any effect, until the server is restarted
15
Slide 16
Slide 16 text
Preloading
# In your INI
opcache.preload=/path/to/preload.php
# In your preload.php
$files = /* files to preload */;
foreach ($files as $file) {
opcache_compile_file($file);
}
16
Slide 17
Slide 17 text
4.
Unpacking in Arrays
Making arrays that much more sweet.
Slide 18
Slide 18 text
Unpacking Arrays
$data = ['b', 'c', 'd'];
$more = ['a', …$data, 'e', 'f'];
# Works with Iterators too
$other = ['a', …$iterator, 'e'];
# But not with Associative Arrays
18
Slide 19
Slide 19 text
Goodbye iterator_to_array();
# These two lines do the same thing
$array = iterator_to_array($iter);
$array = […$iter];
# Again, it doesn’t work with
# associative arrays.
19
Slide 20
Slide 20 text
5.
Arrow Functions
(Short Closures)
One-liner array_maps are easy on the eyes.
Slide 21
Slide 21 text
Arrow Functions / Short Closures
$add = 5;
# Before
array_map(function($num) use ($add) {
return $num + $add;
});
# After
array_map(fn($num) => $num + $add, $numbers);
21
Slide 22
Slide 22 text
6.
Null Coalescing
Assignment
More weapons in the war against NULL.
Slide 23
Slide 23 text
Null Coalescing Operator
// Way Before
$params['greet'] = isset($params['greet'])
? $params['greet']
: 'hello';
// Before
$params['greet'] = $params['greet'] ?? 'hello';
// After
$params['greet'] ??= 'hello';
23
Slide 24
Slide 24 text
7.
Weak References
For more intentional memory optimizations.
Slide 25
Slide 25 text
Weak References
25
Slide 26
Slide 26 text
Weak References
$obj = new Thing();
$ref = new WeakRef($obj);
// Later
if ($ref->valid()) {
$myObj = $ref->get();
} else {
throw new Exception('object destroyed');
}
26
Slide 27
Slide 27 text
8.
Numeric Literal
Separator
Better readability for funky numbers.
Exceptions in __toString
class URL {
// …
public function __toString()
{
throw new Exception('YOLO!');
}
}
// NOT A FATAL ERROR ANYMORE!
30
Slide 31
Slide 31 text
10.
New Custom
Serialization Methods
Do we finally have good/safe serialization?
Slide 32
Slide 32 text
New Serialization
◦ Old way:
◦ __sleep() and __wakeup() methods
◦ Serializable interface
◦ New __serialize() and __unserialize()
◦ Addresses issues with previous implementations
◦ Serializable interface will be deprecated
32
Slide 33
Slide 33 text
New Custom Serialization Methods
class Foo {
private $bar;
public function __serialize(): array {
return ['bar' => $this->bar];
}
public function __unserialize(array $data) {
$this->bar = $data['bar'];
}
}
33
Slide 34
Slide 34 text
34
Thanks!
Any questions?
You can find me at:
◦ @jeremeamia
◦ azPHP Meetups
Slide 35
Slide 35 text
Credits
Special thanks to all the people who made and
released these awesome resources for free:
◦ Presentation template by SlidesCarnival
◦ Photographs by Unsplash
35