PHP8 - What's New In PHP8?
Cheat Sheet
by Dave Child (DaveChild) via cheatography.com/1/cs/27364/
PHP8 Variable Types Return static Attributes
Class/int‐ An instance of the class or class Foo use
erface name interface { App\Attributes\ExampleAttribute;
self The same class the type is public function test(): #[ExampleAttribute]
used in static class Foo
parent The parent of the class the { {
type is used in return new #[ExampleAttribute]
static(); public const FOO =
callable Something callable
} 'foo';
bool A boolean
}
int An integer
#[ExampleAttribute]
Static is now a valid return type.
float A float public $x;
string A string The nullsafe operator (copy)
array An array #[ExampleAttribute]
$foo = $bar->getBaz()?-
iterable Array or traversable public function foo(#[‐
>asFormatted();
ExampleAttribute] $bar) { }
object An object
Quickly check for nulls in method returns. }
mixed Any value
Structured metadata
Return Types Union types
void Function will not return public function foo(Foo|Bar Match expression
anything $input): int|float;
$result = match($input) {
static Function may return You can now specify more than one type in 0 => "hello",
static a type hint: '1', '2', '3' => "wor‐
• The void type cannot be part of a union.
ld",
New mixed type • Unions can be nullable by including null
};
Mixed means any one of: as a type or preceding with a question
Switch statements made simple. Note, the
array bool callable mark.
comparisons made by match use strict
string int resource types.
Named arguments
float null object
function foo(string $a, int $b,
Note that because mixed already contains Constructor property promotion
?string $c = null)
null, it is not nullable itself. class Money
{
{
...
public function __cons‐
}
truct(
foo(
public Currency
c: 'value c',
$currency,
a: 'value a'
public int
);
$amount,
Pass values to functions and methods by
) {}
name, regardless of the order in which they
}
are defined.
In the above example Currency and int
are both automatically made public
properties of the class and assigned values
on construction.
By Dave Child (DaveChild) Published 31st March, 2021. Sponsored by CrosswordCheats.com
cheatography.com/davechild/ Last updated 31st March, 2021. Learn to solve cryptic crosswords!
aloneonahill.com Page 1 of 3. http://crosswordcheats.com
PHP8 - What's New In PHP8? Cheat Sheet
by Dave Child (DaveChild) via cheatography.com/1/cs/27364/
Weak maps ::class on objects New functions
class Foo $foo = new Foo(); str_contains‐ Does haystack
{ var_dump($foo::class); ('haystack', contain needle?
private WeakMap $cache; 'needle')
Works the same as get_class()
.
str_starts_w‐ Does haystack
public function getSom‐ Trailing comma in parameter lists ith('hays‐ start with needle?
ethingWithCaching(object tack', 'needle')
public function(
$obj): object
string $parameterA, str_ends_wit‐ Does haystack
{
int $parameterB, h('haystack', end with needle?
return $this-‐
Foo $objectfoo, 'needle')
>cache[$obj]
) { fdiv() Handles division
??=
// … by zero without an
$this->computeSomethingE‐
} error.
xpensive($obj);
You can now end a parameter list with a get_debug‐ Like gettype()
}
comma without an error. _type() but with more
}
detail
A weak map allows for storage of key-value
Create DateTime objects from interfaces get_resource‐ Fetch the numeric
pairs, but the PHP garbage collection will
_id()
DateTime::createFromInterface(DateTimeInterface ID of a resource
drop a weak map element where the only
reference to it is the weak map itself. $other);
Breaking Changes
DateTimeImmutable::createFromInte‐
Throw expression Internal errors and engine warnings have
rface(DateTimeInterface $other);
been made consistent and/or reclassified.
$triggerError = fn () => throw
New Stringable interface The @ operator no longer silences fatal
new MyError();
$foo = $bar['offset'] ?? throw class Foo errors.
new CustomEx('offset'); { The default error reporting level is now
public function __toSt‐ E_ALL.
Throw is now an expression instead of a
ring(): string The default PDO error mode is no longer
statement, making it more flexible.
{ silent.
return 'foo';
Non-capturing catches Concatenation precedence has changed -
}
try { addition now precedes concatenation.
}
// Something goes wrong Namespaces can now include reserved
function bar(string|Stri‐
} catch (MySpecialException) names.
ngable $stringable) { / … / }
{ ReflectionFunction::isDi‐
bar(new Foo());
Log::error("Some‐ sabled(), ReflectionParam‐
bar('abc');
thing went wrong"); eter::getClass() and Reflec‐
} tionParameter::isCall‐
able() have been deprecated.
When you catch exceptions and throwables,
you no longer need a variable. There are more breaking and engine
changes, this list is limited to those you are
most likely to encounter. For a full run-
down, check out Brent's article linked
below.
By Dave Child (DaveChild) Published 31st March, 2021. Sponsored by CrosswordCheats.com
cheatography.com/davechild/ Last updated 31st March, 2021. Learn to solve cryptic crosswords!
aloneonahill.com Page 2 of 3. http://crosswordcheats.com
PHP8 - What's New In PHP8? Cheat Sheet
by Dave Child (DaveChild) via cheatography.com/1/cs/27364/
With Thanks
This is a condensed version of the excellent
PHP8 write-up you can find by Brent at
stitcher.io
By Dave Child (DaveChild) Published 31st March, 2021. Sponsored by CrosswordCheats.com
cheatography.com/davechild/ Last updated 31st March, 2021. Learn to solve cryptic crosswords!
aloneonahill.com Page 3 of 3. http://crosswordcheats.com