Statement on glibc/iconv Vulnerability

Never

never — тип, который допустимо указывать только как возвращаемое значение, которое указывает, что функция прекратит работу без возврата значения. Она либо вызывает конструкцию языка exit(), либо выбрасывает исключение, либо это бесконечный цикл. Поэтому его нельзя объявлять в объединении типов. Доступно с PHP 8.1.0.

На языке теории типов, never — нижний тип. Это означает, что он — подтип остальных типов и заменяет другие возвращаемые типы при наследовании.

add a note

User Contributed Notes 2 notes

up
14
ali1289445 at gmail dot com
1 year ago
<?php

function sayHello(string $name): never
{
echo
"Hello, $name";
exit();
// if we comment this line, php throws fatal error
}

sayHello("John"); // result: "Hello, John"
up
-5
mateusz dot charytoniuk at protonmail dot com
9 months ago
Overriding the return type of native interfaces:

<?php

class ReadonlyArrayAccess implements ArrayAccess
{
public function
__construct(private readonly $array) {}

public function
offsetExists(mixed $offset): bool
{
return isset(
$this->array[$offset]);
}

public function
offsetGet(mixed $offset): mixed
{
return
$this->array[$offset];
}

public function
offsetSet(mixed $offset, mixed $value): never
{
throw new
LogicException('This array is read only');
}

public function
offsetUnset(mixed $offset): never
{
throw new
LogicException('This array is read only');
}
}
To Top