PHP7
is the new black
Où est PHP6 ?
PHP6 : Unicode
"un seul nombre pour chaque caractère, quelque soit la plate-forme, le programme ou le langage"
RIP
11 march 11:09:37 2010 GMT
PHP 5.4 : The elvis operator ?:
// Avant
$basket = $items ? $items : [];
// Avec Elvis
$basket = $items ?: [];
Spaceship operator
<=>
// Pre Spacefaring^W PHP 7
function order_func($a, $b) {
return ($a < $b) ? -1 : (($a > $b) ? 1 : 0);
}
// Post PHP 7
function order_func($a, $b) {
return $a <=> $b;
}
// Integers
echo 1 <=> 1; // 0
echo 1 <=> 2; // -1
echo 2 <=> 1; // 1
Typage faible : PHPWTF
// Wheeeee
var_dump(1 * '2' + true); //int(3)
var_dump(array(
0 => 0,
'0' => '0', //écrase la valeur précédente
0.0 => 0.0, //écrase la valeur précédente
false => false, //écrase la valeur précédente
));
/*
array (size=1)
0 => boolean false
*/
var_dump((bool) (array) false); //bool(true)
in_array('abc', array(0)); //boolean true
PHP5 : type-hinting
function listenFormation($theodoer)
{
if (!$theodoer->isHungry()) {
return true;
}
return false;
}
listenFormation('kenny');
// PHP Fatal error: Call to a member function isHungry() on string
function listenFormation(Theodoer $theodoer)
{
if (!$theodoer->isHungry()) {
return true;
}
return false;
}
listenFormation('kenny');
// PHP Catchable fatal error: Argument 1 passed to listenFormation()
// must be an instance of Theodoer, string given
// FAIL FAST
PHP7 : scalar type-hinting
function sendHttpStatus(int $statusCode, string $message) {
header('HTTP/1.0 ' .$statusCode. ' ' .$message);
}
sendHttpStatus(404, "File Not Found"); // integer and string passed
sendHttpStatus("coucou", "tu veux voir mon status ?");
// Catchable fatal error: Argument 1 passed to sendHttpStatus() must be of the type integer,
// string given
// So what ?
sendHttpStatus("403", "OK"); // string "403" coerced to int(403)
strict mode
<?php
declare(strict_types=1); // must be the first line
sendHttpStatus(404, "File Not Found"); // integer and string passed
sendHttpStatus("403", "OK");
// Catchable fatal error: Argument 1 passed to sendHttpStatus() must be of the type integer,
// string given
return type declaration
<?php
function get404StatusCode(): int
{
return 404;
}
echo get404StatusCode(); // 404
function get404StatusCode(): int
{
return "quatrecentquatre";
}
echo get404StatusCode(); // Uncaught TypeError: Return value of get404StatusCode()
// must be of the type integer, string returned
Exceptions
<?php
function isTheodoerStillAwake()
{
throw new \Exception();
}
try {
isTheodoerStillAwake();
} catch (\Exception $e) {
}
// Tout va bien
function wakeTheodoer($theodoer)
{
$theodoer->wake();
}
try {
wakeTheodoer('kenny');
} catch (\Exception $e) {
}
// Fatal error: Uncaught exception 'BadMethodCallException' with message
// 'Call to a member function wake() on a non-object
Exceptions
<?php
function wakeTheodoer($theodoer)
{
$theodoer->wake();
}
try {
wakeTheodoer('kenny');
} catch (\Throwable $e) {
}
// Ouf
Ultra fast
php-ng
25 to 70% performance gain
Benchmark
12 Novembre 2015
Symfony 3 !
Bon appétit
PHP7
By Matthieu Auger
PHP7
- 681