Quelle: 5 New Features in PHP 7
ThrowableErrorTypeErrorParseErrorAssertionErrorArithmeticErrorDivisionByZeroErrorset_exception_handler() - Erhält nicht mehr Exception-ObjektException beim fehlerhaften Konstruktor in internen Klassen (früher NULL)Parser error wirft ein ParseError-ObjektE_STRICT-Meldunge wurden auf andere
Level verlegt
list()
list($a[], $a[], $a[]) = [1, 2, 3];
var_dump($a);
$string = "abcde";
list($foo) = $string;
var_dump($foo); // string(1) "a"
foreach ändert nicht mehr den internen Array-Zeiger
$array = [0, 1, 2];
foreach ($array as &$val) {
var_dump(current($array));
}
// PHP 5
int(1)
int(2)
bool(false)
// PHP 7
int(0)
int(0)
int(0)
foreach - Geändertes Verhalten bei Referenzen
$array = [0];
foreach ($array as &$val) {
var_dump($val);
$array[1] = 1;
}
// PHP 5
int(0)
// PHP 7
int(0)
int(1)
call_user_method() und call_user_method_array()mcrypt_generic_end()datefmt_set_timezone_id() und IntlDateFormatter::setTimeZoneID()set_magic_quotes_runtime() und magic_quotes_runtime()imagepsbbox(), imagepsencodefont(), imagepsextendfont(), imagepsfreefont(),
imagepsloadfont(), imagepsslantfont(), imagepstext()<% %><%= %>function sum(int $a, int $b) {
return $a + $b;
}
var_dump(sum(1, 2)); // 3
var_dump(sum(1.5, 2.5)); // 3
declare(strict_types=1);
function sum(int $a, int $b) {
return $a + $b;
}
var_dump(sum(1, 2)); // 3
var_dump(sum(1, 2.5)); Fatal error: Uncaught TypeError: Argument 2 passed to sum() must be of the type int, float given
function sum(int $a, int $b): int {
return $a + $b;
}
var_dump(sum(1, 2)); // 3
var_dump(sum(1.5, 2.5)); // 3
declare(strict_types=1);
function sum(int $a, int $b): int {
return $a + $b;
}
var_dump(sum(1, 2)); // 3
var_dump(sum(1.5, 2.5)); // Fatal error: Uncaught TypeError: Return value of sum() must be of the type integer, float returned
// PHP < 7
$username = isset($_GET['user']) ? $_GET['user'] : 'nobody';
// PHP 7
$username = $_GET['user'] ?? 'nobody';
$username = $_GET['user'] ?? $_POST['user'] ?? 'nobody';
// Integers
echo 1 <=> 1; // 0
echo 1 <=> 2; // -1
echo 2 <=> 1; // 1
// Floats
echo 1.5 <=> 1.5; // 0
echo 1.5 <=> 2.5; // -1
echo 2.5 <=> 1.5; // 1
// Strings
echo "a" <=> "a"; // 0
echo "a" <=> "b"; // -1
echo "b" <=> "a"; // 1
// converts all objects into __PHP_Incomplete_Class object
$data = unserialize($foo, ["allowed_classes" => false]);
// converts all objects into __PHP_Incomplete_Class object except those of MyClass and MyClass2
$data = unserialize($foo, ["allowed_classes" => ["MyClass", "MyClass2"]]);
// default behaviour (same as omitting the second argument) that accepts all classes
$data = unserialize($foo, ["allowed_classes" => true]);
use Declarationen
// PHP 7+ code
use some\namespace\{ClassA, ClassB, ClassC as C};
use function some\namespace\{fn_a, fn_b, fn_c};
use const some\namespace\{ConstA, ConstB, ConstC};
password_hash() entferntdirname() - Neuer Parameter levelspreg_replace() - \e entfernt (Achtung Symfony 1.*!)random_bytes()random_int()error_clear_last()intdiv()preg_replace_callback_array()ReflectionGeneratorReflectionTypeIntlChar