Skip to content

Instantly share code, notes, and snippets.

@ihzarizkyk
Created November 27, 2020 12:15
Show Gist options
  • Save ihzarizkyk/23c4ae84ae478596b8b00005255c1d05 to your computer and use it in GitHub Desktop.
Save ihzarizkyk/23c4ae84ae478596b8b00005255c1d05 to your computer and use it in GitHub Desktop.
<?php
// NAMED ARGUMENTS
// PHP 7
htmlspecialchars($string, ENT_COMPAT | ENT_HTML401, 'UTF-8', false);
// PHP 8
htmlspecialchars($string, double_encode: false);
/****************************************/
// Constructor property promotion
// PHP 7
class Point {
public float $x;
public float $y;
public float $z;
public function __construct(
float $x = 0.0,
float $y = 0.0,
float $z = 0.0,
) {
$this->x = $x;
$this->y = $y;
$this->z = $z;
}
}
// PHP 8
class Point {
public function __construct(
public float $x = 0.0,
public float $y = 0.0,
public float $z = 0.0,
) {}
}
/****************************************/
// Match Expression
// PHP 7
switch (8.0) {
case '8.0':
$result = "Oh no!";
break;
case 8.0:
$result = "This is what I expected";
break;
}
echo $result;
//> Oh no!
// PHP 8
echo match (8.0) {
'8.0' => "Oh no!",
8.0 => "This is what I expected",
};
//> This is what I expected
/****************************************/
// UNION TYPES
// PHP 7
class Number {
/** @var int|float */
private $number;
/**
* @param float|int $number
*/
public function __construct($number) {
$this->number = $number;
}
}
new Number('NaN'); // Ok
// PHP 8
class Number {
public function __construct(
private int|float $number
) {}
}
new Number('NaN'); // TypeError
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment