-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path6_throw_expression.php
More file actions
45 lines (33 loc) · 816 Bytes
/
6_throw_expression.php
File metadata and controls
45 lines (33 loc) · 816 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php
/**
* Copyright (C) 2020 Temuri Takalandze <me@abgeo.dev>
*
* This material can not be copied and/or
* distributed without the express permission of Temuri Takalandze.
*
* Written by Temuri Takalandze <me@abgeo.dev>, November 2020
*/
// Throw exceptions in arrow functions.
$triggerError = fn () => throw new Exception();
$triggerError();
// Throw exceptions with null coalescing operator
function getItem($key) {
$data = [
1 => 'Hello',
2 => 'World',
];
if (!isset($data[$key])) {
throw new Exception("Invalid key '{$key}'!");
}
return $data[$key];
}
// VS
function getItem($key) {
$data = [
1 => 'Hello',
2 => 'World',
];
return $data[$key] ?? throw new Exception("Invalid key '{$key}'!");
}
getItem(1);
getItem(10);