Presented by Giorgio Arcamone
<?php
function addIntegers(int $a, int $b)
{
return $a + $b;
}
var_dump(addIntegers(5, 10)); // int(15)
<?php
function addIntegers(int $a, int $b)
{
return $a + $b;
}
var_dump(addIntegers(5.0, '10')); // still int(15)
<?php
declare(strict_types=1);
function addIntegers(int $a, int $b)
{
return $a + $b;
}
var_dump(addIntegers(5.0, '10')); // TypeError thrown
A few caveats for strict mode:
<?php
function addAnything($a, $b)
{
return $a + $b;
}
declare(strict_types=1); // BAD! - FatalError thrown
function addIntegers(int $a, int $b)
{
return $a + $b;
}
var_dump(addIntegers(5, 10));
<?php
function addIntegers(int $a, int $b): int
{
return $a + $b;
}
var_dump(addIntegers(5, 10));
A few caveats for return types:
<?php
function addIntegers(int $a, int $b): null // FatalError
{
return $a + $b;
}
function addIntegersToo(int $a, int $b): int
{
return 'This is not an integer'; // TypeError
}
(value1) <=> (value2);
<?php
var_dump(5 <=> 5); // 0
var_dump(10 <=> 5); // 1
var_dump(5 <=> 10); // -1
<?php
$distros = [
['name' => 'Debian', 'coolFactor' => 1876],
['name' => 'Mint', 'coolFactor' => 3059],
['name' => 'Ubuntu', 'coolFactor' => 1535],
['name' => 'Arch', 'coolFactor' => 738],
['name' => 'Fedora', 'coolFactor' => 1136]
];
usort($distros, function ($a, $b) { // the old way
if ($a['name'] == $b['name']) {
return 0;
}
return ($a['name'] < $b['name']) ? -1 : 1;
});
usort($distros, function ($a, $b) { // new hotness
return $a['name'] <=> $b['name'];
});
(We used arrays because a mock class didn't fit on one slide...)
A practical example
<?php
// the old way
if (isset($_GET['page']) {
$location = $_GET['page']; // abbreviated example, please avoid doing this
} else {
$location = 'index';
}
// new hotness
$location = $_GET['page'] ?? 'index';
<?php
require 'LinuxDistroInterface.php'; // tricky getting this to fit on one slide!
function processDistro(LinuxDistro $distro)
{
echo $distro->getDistroName() . ' rocks!';
}
processDistro(new class('Debian') implements LinuxDistro {
protected $name;
public function __construct($name)
{
$this->name = $name;
}
public function getDistroName()
{
return $this->name;
}
}); // Debian rocks!
<?php
require './vendor/autoload.php';
use \Foo\Bar\Baz;
use \Foo\Bar\Biz;
use \Foo\Bar\Buz as Buzz;
$baz = new Baz;
$biz = new Biz;
$buz = new Buzz;
<?php
require './vendor/autoload.php';
use \Foo\Bar\{ Baz, Biz, Buz as Buzz };
$baz = new Baz;
$biz = new Biz;
$buz = new Buzz;
Uniform Variable Syntax - everything is now evaluated left-to-right
<?php
$$foo['bar']['baz']; // 5: ${$foo['bar']['baz']} 7: ($$foo)['bar']['baz']
$foo->$bar['baz']; // 5: $foo->{$bar['baz']} 7: ($foo->$bar)['baz']
$foo->$bar['baz'](); // 5: $foo->{$bar['baz']}() 7: ($foo->$bar)['baz']()
Foo::$bar['baz'](); // 5: Foo::{$bar['baz']}() 7: (Foo::$bar)['baz']()
TL;DR: Stop trying to be clever with variable-variables
Errors are now thrown like Exceptions
<?php
function ShouldReturnADataBaseHandle()
{
return null;
}
try {
$db = ShouldReturnADataBaseHandle();
$stmt = $db->prepare('SELECT * FROM users'); // FatalError in 5.6
} catch (Throwable $t) {
echo "Oops, something went wrong: {$t->getMessage()}";
}
ASP and <script> tag support dropped
<% echo 'Hello World'; %> // invalid
<%= 'Hello World'; %> // invalid
<script language="php"> echo 'Hello World'; </script> // invalid
Short tags still work though
<?= 'Hello World'; ?>
<? echo 'Hello World'; ?> // only if short_open_tag is set to On in php.ini