PHP 7 - benvenuto al futuro
-
Upload
christopher-pecoraro -
Category
Engineering
-
view
149 -
download
0
Transcript of PHP 7 - benvenuto al futuro
Il prefisso iper deriva dal greco ὑπερ- e significa sopra oppure oltre. Ha un origine comune al prefisso super usato in
latino.
PHP 4<html>
<head>
<title>PHP Test</title>
</head>
<body>
<?php echo '<p>Hello World</p>'; ?>
</body>
</html>
PHP 5, 5.1, 5.2, 5.3, 5.4, 5.5, 5.6…. finallytry {
echo inverse(5) . "\n";
} catch (Exception $e) {
echo 'Exception: ', $e->getMessage(), "\n";
} finally {
echo "a prescindere\n";
}
PHP 5, 5.1, 5.2, 5.3, 5.4, 5.5, 5.6…. namespace<?php
namespace Marcello;
class Sfincione extends StreetFood{}
<?php
namespace Roberta;
class Sfincione extends StreetFood{}
<?php
namespace Marcello;
class Sfincione extends StreetFood{}
Hello PHP 7● La performance è migliorata: PHP 7 è fino a due volte più veloce di PHP 5.6
● Supporto per 64-bit
● Molti fatal errors sono ora degli Exceptions
● SAPI ed estensioni vecchi e non supportati sono stati rimossi
● L'operatore null coalescing (??)
● L'operatore del "combined comparison" (detto anche "spaceship operator" (<=>)
● Le dichiarazioni dei Return Type
● Le dichiarazioni degli Scalar Type
● Classi anonimi
Scalar type declarations:
● coercitivo (default)
● strict
Scalar types
I seguenti tipi da usare per i parametri possono ora essere forzati:
● string (string)
● integer (int)
● floating-point number (float) nota bene: non numero con la virgola galleggiante
● boolean (bool)
Scalar types
...aumentano i altri tipi che sono stati introdotti in PHP 5:
● class names
● interfaces
● array
● callable
Scalar types (cont.d)
<?php
// modalità coercitivo
function sumOfInts(int ...$ints) // variadic arguments PHP 5.6
{
return array_sum($ints);
}
var_dump(sumOfInts(2, '3', 4.1)); // int, string, float
//risultato: int(9)
Scalar types: esempio coercitivo
<?php
declare(strict_types=1);
// ^^^^^^^^^^^^^^ modalità strict
function sumOfInts(int ...$ints)
{
return array_sum($ints);
}
var_dump(sumOfInts(2, '3', 4.1));
risultato: PHP Fatal error: Uncaught TypeError: Argument 2 passed to sumOfInts() must be of the type integer, string given, called in...
Scalar types: esempio "strict"
<?php
// modalità Coercitivo
function sumOfInts(int ...$ints) : int
{
return array_sum($ints);
}
var_dump(sumOfInts(2, '3', 4.1));
//risultato: int(9)
Scalar return types: esempio coercitivo
<?php
declare(strict_types=1);
// ^^^^^^^^^^^^^^ modalità strict
function sumOfInts(int ...$ints) : int
{
return array_sum($ints);
}
var_dump(sumOfInts(2, 3, 4)); // int, int, int
//risultato: int(9)
Scalar return types: esempio "strict"
<?php
declare(strict_types=1);
// ^^^^^^^^^^^^^^ modalità strict
function sumOfInts(int ...$ints) : string
{
return array_sum($ints);
}
var_dump(sumOfInts(2, 3, 4)); // int, int, int
risultato: PHP Fatal error: Uncaught TypeError: Return value of sumOfInts() must be of the type string, integer returned
Scalar return types: esempio "strict"
spaceship operatorfunction myfunction($var1, $var2) { return ($var1 < $var2) ? -1 : (($var1 > $var2) ? 1 : 0);}
diventa
function myfunction($var1, $var2) { return $var1 <=> $var2;}
coalescing operator$quelloCheVoglio = isset($chistu) ? $chiustu : $chiddu;
//diventa
$quelloCheVoglio = $chistu ?? $chiddu;
import groupsuse MyCompany\MyCompany\PackageA\MyClass;
use MyCompany\MyCompany\PackageB\MyClass;
use MyCompany\MyCompany\PackageC\MyClass;
diventa
use MyCompany\MyPackage\{
PackageA\MyClass,
PackageB\MyClass,
PackageC\MyClass
};