Documentation

Header
in package
implements ArrayAccess, IteratorAggregate, Countable

FinalYes

HeaderCollection

  • Immutable by default optionally (pass $immutable = false to mutate in-place)
  • Uses case-insensitive header keys internally, preserves original-case for output

Comments and docblocks in English.

Table of Contents

Interfaces

ArrayAccess
IteratorAggregate
Countable

Properties

$headerCallbackRegistered  : bool
Static flag to ensure header_register_callback() is called only once per PHP request lifecycle
$immutable  : bool
$map  : array<string, array<string|int, string>>
$originalNames  : array<string, string>

Methods

__construct()  : mixed
add()  : static
Add header value(s) without removing existing ones.
addRawLine()  : void
Parse a raw header line like "Name: value" and add to collection.
count()  : int
Return count of distinct header names.
fromGlobals()  : self
Create from PHP globals / server variables (fallback polyfill for getallheaders).
fromPsr7()  : self
Convert PSR-7 MessageInterface headers into HeaderCollection
get()  : string|null
Get first header value as string (joins multiple values with comma if present).
getAll()  : array<string|int, string>
Get all values for header as array.
getIterator()  : ArrayIterator<string, array<string|int, string>>
IteratorAggregate: iterate as name => values[] Preserves canonical name casing for keys.
has()  : bool
offsetExists()  : bool
offsetGet()  : mixed
offsetSet()  : void
offsetUnset()  : void
parseAccept()  : array<string|int, mixed>
parseAcceptEncoding()  : array<string|int, mixed>
parseAcceptLanguage()  : array<string|int, mixed>
parseCacheControl()  : array<string, string|bool>
Parse Cache-Control header into associative flags/values.
parseContentType()  : array<string|int, mixed>|null
Parse and return Content-Type header structured info: ['mime'=>'text/html', 'params' => ['charset' => 'utf-8']]
prepend()  : static
Prepend a value for a header (useful for Cache-Control ordering).
registerHeaderCallback()  : bool
Register a single header_register_callback for this request lifecycle.
remove()  : static
Remove header entirely.
send()  : void
Send headers to client using header() and optionally set response code.
set()  : static
Set header (replace existing values). Accepts string or array of strings.
toArray()  : array<string, array<string|int, string>>
Return array representation: canonical name => array of values
toLines()  : array<string|int, string>
Return header lines: ["Name: value", ...] If header has multiple values, each becomes its own line.
toPsr7()  : MessageInterface
Apply headers from this collection to a PSR-7 MessageInterface instance.
toString()  : string
Return raw string representation (CRLF separated).
withImmutable()  : self
Convenience: create a copy that is immutable.
__clone()  : mixed
canonicalizeName()  : string
Canonicalize header name for output: Title-Case (Content-Type)
fromServerSuperglobals()  : array<string|int, mixed>
Polyfill to extract headers from $_SERVER (works on most SAPI).
mutableInstance()  : self
Helper: create a mutable copy or return $this according to $this->immutable.
normalizeKey()  : string
Normalize header key to lowercase trimmed.
parseWeightedHeaderRaw()  : array<string|int, mixed>
Parse Accept-like headers (Accept, Accept-Language, Accept-Encoding) Returns array of ['value' => string, 'q' => float] ordered by q desc then appearance order

Properties

$headerCallbackRegistered

Static flag to ensure header_register_callback() is called only once per PHP request lifecycle

private static bool $headerCallbackRegistered = false

$immutable

private bool $immutable

$map

private array<string, array<string|int, string>> $map = []

Lower-cased header name => array of values

$originalNames

private array<string, string> $originalNames = []

Lower-cased header name => original canonical case ("Content-Type")

Methods

__construct()

public __construct([array<string, string|array<string|int, string>> $headers = [] ][, bool $immutable = false ][, bool $registerHeaderCallback = true ]) : mixed
Parameters
$headers : array<string, string|array<string|int, string>> = []

initial headers (name => value or array of values)

$immutable : bool = false

if true methods like set/add/remove will return new instance instead of mutating

$registerHeaderCallback : bool = true

add()

Add header value(s) without removing existing ones.

public add(string $name, string|array<string|int, string> $value) : static
Parameters
$name : string
$value : string|array<string|int, string>
Return values
static

addRawLine()

Parse a raw header line like "Name: value" and add to collection.

public addRawLine(string $line) : void
Parameters
$line : string

count()

Return count of distinct header names.

public count() : int
Return values
int

fromGlobals()

Create from PHP globals / server variables (fallback polyfill for getallheaders).

public static fromGlobals([bool $immutable = false ]) : self
Parameters
$immutable : bool = false
Return values
self

fromPsr7()

Convert PSR-7 MessageInterface headers into HeaderCollection

public static fromPsr7(MessageInterface $message[, bool $immutable = false ]) : self
Parameters
$message : MessageInterface
$immutable : bool = false
Return values
self

get()

Get first header value as string (joins multiple values with comma if present).

public get(string $name[, string|null $default = null ]) : string|null
Parameters
$name : string
$default : string|null = null
Return values
string|null

getAll()

Get all values for header as array.

public getAll(string $name) : array<string|int, string>
Parameters
$name : string
Return values
array<string|int, string>

getIterator()

IteratorAggregate: iterate as name => values[] Preserves canonical name casing for keys.

public getIterator() : ArrayIterator<string, array<string|int, string>>
Return values
ArrayIterator<string, array<string|int, string>>

has()

public has(string $name) : bool
Parameters
$name : string
Return values
bool

offsetExists()

public offsetExists(mixed $offset) : bool
Parameters
$offset : mixed
Return values
bool

offsetGet()

public offsetGet(mixed $offset) : mixed
Parameters
$offset : mixed

offsetSet()

public offsetSet(mixed $offset, mixed $value) : void
Parameters
$offset : mixed
$value : mixed

offsetUnset()

public offsetUnset(mixed $offset) : void
Parameters
$offset : mixed

parseAccept()

public parseAccept() : array<string|int, mixed>
Return values
array<string|int, mixed>

parseAcceptEncoding()

public parseAcceptEncoding() : array<string|int, mixed>
Return values
array<string|int, mixed>

parseAcceptLanguage()

public parseAcceptLanguage() : array<string|int, mixed>
Return values
array<string|int, mixed>

parseCacheControl()

Parse Cache-Control header into associative flags/values.

public parseCacheControl() : array<string, string|bool>

E.g. "max-age=3600, public" => ['max-age' => '3600', 'public' => true]

Return values
array<string, string|bool>

parseContentType()

Parse and return Content-Type header structured info: ['mime'=>'text/html', 'params' => ['charset' => 'utf-8']]

public parseContentType() : array<string|int, mixed>|null
Return values
array<string|int, mixed>|null

prepend()

Prepend a value for a header (useful for Cache-Control ordering).

public prepend(string $name, string $value) : static
Parameters
$name : string
$value : string
Return values
static

registerHeaderCallback()

Register a single header_register_callback for this request lifecycle.

public registerHeaderCallback([callable|null $callback = null ]) : bool

Returns true if callback was registered now, false if it had been registered earlier.

Parameters
$callback : callable|null = null
Return values
bool

remove()

Remove header entirely.

public remove(string $name) : static
Parameters
$name : string
Return values
static

send()

Send headers to client using header() and optionally set response code.

public send([bool $replace = true ][, int|null $responseCode = null ]) : void

Replaces or appends depending on $replace param.

Parameters
$replace : bool = true
$responseCode : int|null = null

set()

Set header (replace existing values). Accepts string or array of strings.

public set(string $name, string|array<string|int, string> $value) : static

If immutable mode is enabled, returns new instance. Otherwise returns $this.

Parameters
$name : string
$value : string|array<string|int, string>
Return values
static

toArray()

Return array representation: canonical name => array of values

public toArray() : array<string, array<string|int, string>>
Return values
array<string, array<string|int, string>>

toLines()

Return header lines: ["Name: value", ...] If header has multiple values, each becomes its own line.

public toLines() : array<string|int, string>
Return values
array<string|int, string>

toPsr7()

Apply headers from this collection to a PSR-7 MessageInterface instance.

public toPsr7(MessageInterface $message) : MessageInterface

Returns a new MessageInterface instance (PSR-7 objects are immutable).

Parameters
$message : MessageInterface
Return values
MessageInterface

toString()

Return raw string representation (CRLF separated).

public toString([string $eol = "\r\n" ]) : string
Parameters
$eol : string = "\r\n"
Return values
string

withImmutable()

Convenience: create a copy that is immutable.

public withImmutable([bool $immutable = true ]) : self
Parameters
$immutable : bool = true
Return values
self

__clone()

private __clone() : mixed

canonicalizeName()

Canonicalize header name for output: Title-Case (Content-Type)

private static canonicalizeName(string $name) : string
Parameters
$name : string
Return values
string

fromServerSuperglobals()

Polyfill to extract headers from $_SERVER (works on most SAPI).

private static fromServerSuperglobals() : array<string|int, mixed>
Return values
array<string|int, mixed>

mutableInstance()

Helper: create a mutable copy or return $this according to $this->immutable.

private mutableInstance() : self
Return values
self

normalizeKey()

Normalize header key to lowercase trimmed.

private static normalizeKey(string $name) : string
Parameters
$name : string
Return values
string

parseWeightedHeaderRaw()

Parse Accept-like headers (Accept, Accept-Language, Accept-Encoding) Returns array of ['value' => string, 'q' => float] ordered by q desc then appearance order

private parseWeightedHeaderRaw(string $raw) : array<string|int, mixed>
Parameters
$raw : string
Return values
array<string|int, mixed>
On this page

Search results