La traduction de l'article a été préparée à la veille du début du cours "Développeur backend en PHP" .
Aide-mémoire sur les changements PHP v7.x
PHP 7.3 PHP: .
, PHP PHP v7.x, - , , , - .
PHP 5.6 , . , , - .
PHP 7.0
:
new class($i) {
public function __construct($i) {
$this->i = $i;
}
}
- ( 0).
. ( ) , EWARNING FALSE.
intdiv(int $numerator, int $divisor)
null - «??»
$x = NULL;
$y = NULL;
$z = 3;
vardump($x ?? $y ?? $z); // int(3)
$x = ["c" => "meaningfulvalue"];
vardump($x["a"] ?? $x["b"] ?? $x["c"]); // string(16) "meaningfulvalue"
- spaceship ( ) (<=>)
.
//
f unction orderfunc($a, $b) {
return ($a < $b) ? -1 : (($a > $b) ? 1 : 0);
}
// <=>
function orderfunc($a, $b) {
return $a <=> $b;
}
PHP - v0.5.
function add(float $a, float $b): float {
return $a + $b;
}
add(1, 2); // float(3)
- , . , ( v7.1 :))
interface A {
static function make(): A;
}
class B implements A {
static function make(): A {
return new B();
}
}
// use :
use FooLibrary\Bar\Baz\ClassA;
use FooLibrary\Bar\Baz\ClassB;
use FooLibrary\Bar\Baz\ClassC;
use FooLibrary\Bar\Baz\ClassD as Fizbo;
// use :
use FooLibrary\Bar\Baz{ ClassA, ClassB, ClassC, ClassD as Fizbo };
:
yield from <expr>
PHP 7 , PHP 5.6.
, PHP 7.0 . 7.0.0 3 , 5.6 opcache 2.7 opcache! !
, , .
Error
Exception
Throwable
.
Throwable
:
interface Throwable
|- Error implements Throwable
|- ArithmeticError extends Error
|- DivisionByZeroError extends ArithmeticError
|- AssertionError extends Error
|- ParseError extends Error
|- TypeError extends Error
|- ArgumentCountError extends TypeError
|- Exception implements Throwable
|- ClosedGeneratorException extends Exception
|- DOMException extends Exception
|- ErrorException extends Exception
|- IntlException extends Exception
|- LogicException extends Exception
|- BadFunctionCallException extends LogicException
|- BadMethodCallException extends BadFunctionCallException
|- DomainException extends LogicException
|- InvalidArgumentException extends LogicException
|- LengthException extends LogicException
|- OutOfRangeException extends LogicException
|- PharException extends Exception
|- ReflectionException extends Exception
|- RuntimeException extends Exception
|- OutOfBoundsException extends RuntimeException
|- OverflowException extends RuntimeException
|- PDOException extends RuntimeException
|- RangeException extends RuntimeException
|- UnderflowException extends RuntimeException
|- UnexpectedValueException extends RuntimeException
⚠ ! Throwable
Error
Exception
.
Unicode — “\u{xxxxx}”
echo "\u{202E}Reversed text"; // Reversed text
echo "mañana"; // "ma\u{00F1}ana"
echo "mañana"; // "man\u{0303}ana" "n" ~ (U+0303)
-:
callable class trait extends implements static abstract final public protected private const
enddeclare endfor endforeach endif endwhile and global goto instanceof insteadof interface
namespace new or xor try use var exit list clone include includeonce throw array
print echo require requireonce return else elseif default break continue switch yield
function if endswitch finally for foreach declare case do while as catch die self parent
, - class
- ::class
.
PHP 7.1
function answer(): ?int {
return null; //
}
function answer(): ?int {
return 42; //
}
function answer(): ?int {
return new stdclass(); //
}
function say(?string $msg) {
if ($msg) {
echo $msg;
}
}
say('hello'); // - hello
say(null); // -
say(); // -
say(new stdclass); // -
function shouldreturnnothing(): void {
return 1; // Fatal error: A void function must not return a value
}
, , , , .
void
void , :
function lacksreturn(): void {
//
}
array
, Traversable
, foreach
. , array
, Traversable
- , , , .
function foo(iterable $iterable) {
foreach ($iterable as $value) {
// …
}
}
iterable
, , . Traversable
, TypeError
.
function bar(): iterable {
return [1, 2, 3];
}
, iterable
null
.
function foo(iterable $iterable = []) {
// …
}
class Closure {
…
public static function fromCallable(callable $callable) : Closure {…}
…
}
$array = [1, 2, 3];
// $a, $b $c $array ,
[$a, $b, $c] = $array;
// $a, $b $c $array "a", "b" "c"
["a" => $a, "b" => $b, "c" => $c] = $array;
list()
$powersOfTwo = [1 => 2, 2 => 4, 3 => 8];
list(1 => $oneBit, 2 => $twoBit, 3 => $threeBit) = $powersOfTwo;
class Token {
// public
const PUBLICCONST = 0;
//
private const PRIVATECONST = 0;
protected const PROTECTEDCONST = 0;
public const PUBLICCONSTTWO = 0;
//
private const FOO = 1, BAR = 2;
}
try {
// - …
} catch (ExceptionType1 | ExceptionType2 $e) {
//
} catch (\Exception $e) {
// …
}