Last active
October 21, 2016 08:52
-
-
Save MasterHans/ac7b2d73b6d8ba7553111350c86a1496 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1. | |
Было: | |
use Framework\Module\Foo; | |
use Framework\Module\Bar; | |
use Framework\Module\Baz; | |
В PHP 7 можно написать: | |
use Framework\Module\{Foo, Bar, Baz}; | |
2. | |
До PHP 7: | |
if (isset($foo)) { | |
$bar = $foo; | |
} else { | |
$bar = 'default'; // присваиваем $bar значение 'default' если $foo равен NULL | |
} | |
Или так: | |
$bar = isset($foo) ? $foo : 'default'; | |
В PHP 7: | |
$bar = $foo ?? 'default'; | |
3. | |
switch ($bar <=> $foo) { | |
case 0: | |
echo '$bar и $foo равны'; | |
case -1: | |
echo '$foo больше'; | |
case 1: | |
echo '$bar больше'; | |
} | |
4. Типы данных в качестве параметров | |
class Calculator | |
{ | |
// объявляем, что параметры имеют целый тип integer | |
public function addTwoInts(int $x, int $y): int { | |
// явно объявляем, что метод возвращает целое | |
return $x + $y; | |
} | |
} | |
5. Анонимные классы | |
class MyLogger { | |
public function log($msg) { | |
print_r($msg . "\n"); | |
} | |
} | |
$pusher->setLogger( new MyLogger() ); | |
6. Функции CSPRNG | |
Две новых функции для генерации крипографически безопасной строки и целых. | |
Первая возвращает случайную строку длиной $len: | |
random_bytes(int $len); | |
Вторая возвращает число в диапазоне $min… $max. | |
random_int(int $min, int $max); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment