Oihana PHP Enums

HttpStatusCode uses ConstantsTrait

Enumeration of standard HTTP status codes.

This class provides a centralized, type-safe list of common HTTP status codes, preserving the exact numeric values defined by the relevant RFCs (notably RFC 7231 and RFC 9110).

Usage examples:

  • Access a status code: HttpStatusCode::OK
  • Validate/inspect available codes with ConstantsTrait utilities:
    • HttpStatusCode::enums() returns all status code values
    • HttpStatusCode::includes(200) checks existence
    • HttpStatusCode::getConstant(200) returns the constant name

Notes:

  • Only widely used standardized status codes are listed.

Table of Contents

Constants

ACCEPTED  : int = 202
ALREADY_REPORTED  : int = 208
BAD_GATEWAY  : int = 502
BAD_REQUEST  : int = 400
BANDWIDTH_LIMIT_EXCEEDED  : int = 509
BUSY  : int = 600
CONFLICT  : int = 409
CONTINUE  : int = 100
CREATED  : int = 201
DEFAULT  : int = 0
DISCONNECTED_OPERATION  : int = 112
EARLY_HINTS  : int = 103
EXPECTATION_FAILED  : int = 417
FAILED_DEPENDENCY  : int = 424
FORBIDDEN  : int = 403
FOUND  : int = 302
GATEWAY_TIMEOUT  : int = 504
GONE  : int = 410
HEURISTIC_EXPIRATION  : int = 113
HTTP_REQUEST_SENT_TO_HTTPS_PORT  : int = 497
HTTP_VERSION_NOT_SUPPORTED  : int = 505
IM_A_TEAPOT  : int = 418
IM_USED  : int = 226
INSUFFICIENT_STORAGE  : int = 507
INTERNAL_SERVER_ERROR  : int = 500
INVALID_TOKEN  : int = 498
LENGTH_REQUIRED  : int = 411
LOCKED  : int = 423
LOGIN_TIMEOUT  : int = 440
LOOP_DETECTED  : int = 508
METHOD_NOT_ALLOWED  : int = 405
MISCELLANEOUS_PERSISTENT_WARNING  : int = 299
MISCELLANEOUS_WARNING  : int = 199
MISDIRECTED_REQUEST  : int = 421
MOVED_PERMANENTLY  : int = 301
MULTI_STATUS  : int = 207
MULTIPLE_CHOICES  : int = 300
NETWORK_AUTHENTICATION_REQUIRED  : int = 511
NETWORK_CONNECTION_TIMEOUT_ERROR  : int = 599
NO_CONTENT  : int = 204
NON_AUTHORITATIVE_INFORMATION  : int = 203
NOT_ACCEPTABLE  : int = 406
NOT_EXTENDED  : int = 510
NOT_FOUND  : int = 404
NOT_IMPLEMENTED  : int = 501
NOT_MODIFIED  : int = 304
OK  : int = 200
PARTIAL_CONTENT  : int = 206
PAYLOAD_TOO_LARGE  : int = 413
PAYMENT_REQUIRED  : int = 402
PERMANENT_REDIRECT  : int = 308
PRECONDITION_FAILED  : int = 412
PRECONDITION_REQUIRED  : int = 428
PROCESSING  : int = 102
PROXY_AUTHENTICATION_REQUIRED  : int = 407
RANGE_NOT_SATISFIABLE  : int = 416
REQUEST_HEADER_FIELDS_TOO_LARGE  : int = 431
REQUEST_TIMEOUT  : int = 408
RESET_CONTENT  : int = 205
RESPONSE_IS_STALE  : int = 110
REVALIDATION_FAILED  : int = 111
SEE_OTHER  : int = 303
SERVICE_UNAVAILABLE  : int = 503
SWITCH_PROXY  : int = 306
SWITCHING_PROTOCOLS  : int = 101
TEMPORARY_REDIRECT  : int = 307
TOKEN_REQUIRED  : int = 499
TOO_EARLY  : int = 425
TOO_MANY_REQUESTS  : int = 429
TRANSFORMATION_APPLIED  : int = 214
UNAUTHORIZED  : int = 401
UNAVAILABLE_FOR_LEGAL_REASONS  : int = 451
UNPROCESSABLE_ENTITY  : int = 422
UNSUPPORTED_MEDIA_TYPE  : int = 415
UPGRADE_REQUIRED  : int = 426
URI_TOO_LONG  : int = 414
USE_PROXY  : int = 305
VARIANT_ALSO_NEGOTIATES  : int = 506

Methods

fromException()  : int
Returns the HTTP status code carried by `$e->getCode()` when it falls in the 4xx/5xx range — the convention used by oihana `Error4xx`/`Error5xx` exceptions which pass their status as the second argument of `Exception::__construct( $message , $code )`.
getDescription()  : string|null
Returns the standard HTTP status description for a given code.
getType()  : string|null
Returns the status type corresponding to a given HTTP status code.

Constants

BANDWIDTH_LIMIT_EXCEEDED

public int BANDWIDTH_LIMIT_EXCEEDED = 509

HTTP_REQUEST_SENT_TO_HTTPS_PORT

public int HTTP_REQUEST_SENT_TO_HTTPS_PORT = 497

HTTP_VERSION_NOT_SUPPORTED

public int HTTP_VERSION_NOT_SUPPORTED = 505

MISCELLANEOUS_PERSISTENT_WARNING

public int MISCELLANEOUS_PERSISTENT_WARNING = 299

NETWORK_AUTHENTICATION_REQUIRED

public int NETWORK_AUTHENTICATION_REQUIRED = 511

NETWORK_CONNECTION_TIMEOUT_ERROR

public int NETWORK_CONNECTION_TIMEOUT_ERROR = 599

NON_AUTHORITATIVE_INFORMATION

public int NON_AUTHORITATIVE_INFORMATION = 203

PROXY_AUTHENTICATION_REQUIRED

public int PROXY_AUTHENTICATION_REQUIRED = 407

REQUEST_HEADER_FIELDS_TOO_LARGE

public int REQUEST_HEADER_FIELDS_TOO_LARGE = 431
public int UNAVAILABLE_FOR_LEGAL_REASONS = 451

VARIANT_ALSO_NEGOTIATES

public int VARIANT_ALSO_NEGOTIATES = 506

Methods

fromException()

Returns the HTTP status code carried by `$e->getCode()` when it falls in the 4xx/5xx range — the convention used by oihana `Error4xx`/`Error5xx` exceptions which pass their status as the second argument of `Exception::__construct( $message , $code )`.

public static fromException(Throwable $e) : int

Returns HttpStatusCode::INTERNAL_SERVER_ERROR otherwise, so unexpected runtime failures (PHP Exception with default 0, driver-level exceptions with custom codes, etc.) still surface as 500.

Use it in catch( Throwable ) blocks of controllers to keep the fail() response aligned with the actual error class, instead of collapsing every 403/404/409/422/… to an opaque 500. Without this, an Error403( 'Forbidden field: status' ) thrown by an inner capability check arrives at the outer catch and the controller loses the HTTP code before responding — masking the real authorisation outcome from both the client and the audit log.

Parameters
$e : Throwable
Tags
example
catch( Throwable $e )
{
    return $this->fail
    (
        request  : $request ,
        response : $response ,
        code     : HttpStatusCode::fromException( $e ) ,
        details  : $e->getMessage() ,
    ) ;
}
Return values
int

getDescription()

Returns the standard HTTP status description for a given code.

public static getDescription(int|string $code) : string|null

The method normalizes the input to an integer and returns the corresponding textual reason phrase (e.g., "OK", "Not Found", "Internal Server Error"), based on the defined constants in this class.

The mapping includes:

  • 1xx (100–199): Informational responses (e.g., "Continue", "Processing").
  • 2xx (200–299): Successful responses (e.g., "OK", "Created").
  • 3xx (300–399): Redirection messages (e.g., "Moved Permanently", "See Other").
  • 4xx (400–499): Client error responses (e.g., "Bad Request", "Not Found").
  • 5xx (500–599): Server error responses (e.g., "Internal Server Error", "Bad Gateway").
  • Custom codes: Application-specific or extended values (e.g., "Busy").

If the given code does not match any predefined constant, the method will return null.

Parameters
$code : int|string

The HTTP status code to evaluate. Strings are cast to integers.

Return values
string|null

The human-readable status description associated with the code, or null if no match is found.

getType()

Returns the status type corresponding to a given HTTP status code.

public static getType(int|string $code) : string|null

The method normalizes the input to an integer and determines the category of the HTTP response based on its range:

Any value outside these ranges will return null.

Parameters
$code : int|string

The HTTP status code to evaluate. Strings are cast to integers.

Return values
string|null

One of the Output::* constants (INFO, SUCCESS, REDIRECT, ERROR) if the code matches a known range, or null otherwise.

On this page

Search results