Throw

정실버·2022년 7월 4일
0

php8

목록 보기
6/6

PHP8 이전에는 throw는 명령문이므로 화살표 함수, 삼항연산자, 엘비스 연산자(?:)와 같이 표현식만 허용되는 위치에서 예외를 throw 하는 것은 불가능했으나 PHP8에서는 가능해짐

// This was previously not possible since arrow functions only accept a single expression while throw was a statement.
$callable = fn() => throw new Exception();
 
// $value is non-nullable.
$value = $nullableValue ?? throw new InvalidArgumentException();
 
// $value is truthy.
$value = $falsableValue ?: throw new InvalidArgumentException();
 
// $value is only set if the array is not empty.
$value = !empty($array)
    ? reset($array)
    : throw new InvalidArgumentException();
profile
백엔드. PHP. NodeJS

0개의 댓글