0% found this document useful (0 votes)
6 views

Davechild - php8 What S New in php8

PHP8 - What's New In PHP8? Cheat Sheet

Uploaded by

blackchorimarock
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Davechild - php8 What S New in php8

PHP8 - What's New In PHP8? Cheat Sheet

Uploaded by

blackchorimarock
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

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(): #[Exam​ple​Att​ribute]
used in static class Foo

parent The parent of the class the ​ ​ ​ { {


type is used in ​ ​ ​ ​ ​ ​ ​ ​return new ​ ​ ​ ​#[E​xam​ple​Att​ribute]
static(); ​ ​ ​ ​public const FOO =
callable Something callable
​ ​ ​ } 'foo';
bool A boolean
}
int An integer
​ ​ ​ ​#[E​xam​ple​Att​ribute]
Static is now a valid return type.
float A float ​ ​ ​ ​public $x;
string A string The nullsafe operator (copy)
array An array ​ ​ ​ ​#[E​xam​ple​Att​ribute]
$foo = $bar->getBaz()?-
iterable Array or traver​sable ​ ​ ​ ​public function foo(#[​‐
>asFormatted();
Exa​mpl​eAt​tri​bute] $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 => "​hel​lo",
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.
compar​isons 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 Constr​uctor 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 automa​tically made public
properties of the class and assigned values
on constr​uction.

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_co​nta​ins​‐ Does haystack


{ var_du​mp(​$fo​o::​class); ('h​ays​tack', contain needle?
​ ​ ​ ​private WeakMap $cache; 'needle')
Works the same as get_cl​ass()
.
str_st​art​s_w​‐ Does haystack
​ ​ ​ ​public function getSom​‐ Trailing comma in parameter lists ith​('h​ays​‐ start with needle?
eth​ing​Wit​hCa​chi​ng(​object tack', 'needle')
public function(
$obj): object
​ ​ ​ ​string $param​eterA, str_en​ds_​wit​‐ Does haystack
​ ​ ​ {
​ ​ ​ int $param​eterB, h('​hay​stack', end with needle?
​ ​ ​ ​ ​ ​ ​ ​return $this-​‐
​ ​ ​ Foo $objec​tfoo, 'needle')
>ca​che​[$obj]
) { fdiv() Handles division
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ??=
​ ​ ​ // … by zero without an
$this-​>co​mpu​teS​ome​thi​ngE​‐
} error.
xpe​nsi​ve(​$obj);
You can now end a parameter list with a get_de​bug​‐ 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_re​sou​rce​‐ 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
DateTi​meI​mmu​tab​le:​:cr​eat​eFr​omI​nte​‐
Throw expression Internal errors and engine warnings have
rfa​ce(​Dat​eTi​meI​nte​rface $other);
been made consistent and/or reclas​sified.
$triggerError = fn () => throw
New Stringable interface The @ operator no longer silences fatal
new MyError();
$foo = $bar['​off​set'] ?? throw class Foo errors.

new Custom​Ex(​'of​fset'); { 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-ca​pturing catches Concat​enation precedence has changed -
​ ​ ​ }
try { addition now precedes concat​ena​tion.
}
​ ​ ​ // Something goes wrong Namespaces can now include reserved
function bar(st​rin​g|S​tri​‐
} catch (MySpe​cia​lEx​cep​tion) names.
ngable $strin​gable) { / … / }
{ Reflec​tio​nFu​nct​ion​::i​sDi​‐
bar(new Foo());
​ ​ ​ ​Log​::e​rro​r("S​ome​‐ sab​led(), Reflec​tio​nPa​ram​‐
bar('a​bc');
thing went wrong"); ete​r::​get​Class() and Reflec​‐
} tio​nPa​ram​ete​r::​isC​all​‐
able() have been deprec​ated.
When you catch exceptions and throwa​bles,
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
stitch​er.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

You might also like