Skip to content
PHP Ecosystem
Explore

HeaderCollection

API reference › yii › web › HeaderCollection

HeaderCollection is used by Response to maintain the currently registered HTTP headers.

PropertyTypeDescriptionDefined by
$count read-onlyintThe number of headers in the collection.HeaderCollection
$iterator read-onlyArrayIterator<string, string[]>An iterator for traversing the headers in the collection.HeaderCollection
MethodDescriptionDefined by
__construct()Constructor.yii\base\BaseObject
__call()Calls the named method which is not a class method.yii\base\BaseObject
__get()Returns the value of an object property.yii\base\BaseObject
__isset()Checks if a property is set, i.e. defined and not null.yii\base\BaseObject
__set()Sets value of an object property.yii\base\BaseObject
__unset()Sets an object property to null.yii\base\BaseObject
add()Adds a new header.HeaderCollection
canGetProperty()Returns a value indicating whether a property can be read.yii\base\BaseObject
canSetProperty()Returns a value indicating whether a property can be set.yii\base\BaseObject
className() staticReturns the fully qualified name of this class.yii\base\BaseObject
count()Returns the number of headers in the collection.HeaderCollection
fromArray()Populates the header collection from an array.HeaderCollection
get()Returns the named header(s).HeaderCollection
getCount()Returns the number of headers in the collection.HeaderCollection
getIterator()Returns an iterator for traversing the headers in the collection.HeaderCollection
has()Returns a value indicating whether the named header exists.HeaderCollection
hasMethod()Returns a value indicating whether a method is defined.yii\base\BaseObject
hasProperty()Returns a value indicating whether a property is defined.yii\base\BaseObject
init()Initializes the object.yii\base\BaseObject
offsetExists()Returns whether there is a header with the specified name.HeaderCollection
offsetGet()Returns the header with the specified name.HeaderCollection
offsetSet()Adds the header to the collection.HeaderCollection
offsetUnset()Removes the named header.HeaderCollection
remove()Removes a header.HeaderCollection
removeAll()Removes all headers.HeaderCollection
set()Adds a new header.HeaderCollection
setDefault()Sets a new header only if it does not exist yet.HeaderCollection
toArray()Returns the collection as a PHP array.HeaderCollection
toOriginalArray()Returns the collection as a PHP array but instead of using normalized header names as keys (like toArray()) it uses original header names (case-sensitive).HeaderCollection
public function add(string $name, string $value): $this

Adds a new header.

If there is already a header with the same name, the new one will be appended to it instead of replacing it.

Parameters

ParameterTypeDescription
$namestringThe name of the header
$valuestringThe value of the header

Returns

$this: The collection object itself

public function count(): int

Returns the number of headers in the collection.

This method is required by the SPL Countable interface. It will be implicitly called when you use count($collection).

Returns

int: The number of headers in the collection.

public function fromArray(array $array): mixed

Populates the header collection from an array.

Parameters

ParameterTypeDescription
$arrayarrayThe headers to populate from

Returns

mixed

Since

2.0.3

public function get(string $name, ($first is true ? string|null : string[]|null) $default = null, bool $first = true): ($first is true ? string|null : string[]|null)

Returns the named header(s).

Parameters

ParameterTypeDescription
$namestringThe name of the header to return
$default($first is true ? string|null : string[]|null)The value to return in case the named header does not exist
$firstboolWhether to only return the first header of the specified name. If false, all headers of the specified name will be returned.

Returns

($first is true ? string|null : string[]|null): The named header(s). If $first is true, a string will be returned; If $first is false, an array will be returned.

public function getCount(): int

Returns the number of headers in the collection.

Returns

int: The number of headers in the collection.

public function getIterator(): ArrayIterator<string, string[]>

Returns an iterator for traversing the headers in the collection.

This method is required by the SPL interface IteratorAggregate. It will be implicitly called when you use foreach to traverse the collection.

Returns

ArrayIterator<string, string[]>: An iterator for traversing the headers in the collection.

public function has(string $name): bool

Returns a value indicating whether the named header exists.

Parameters

ParameterTypeDescription
$namestringThe name of the header

Returns

bool: Whether the named header exists

public function offsetExists(string $name): bool

Returns whether there is a header with the specified name.

This method is required by the SPL interface ArrayAccess. It is implicitly called when you use something like isset($collection[$name]).

Parameters

ParameterTypeDescription
$namestringThe header name

Returns

bool: Whether the named header exists

public function offsetGet(string $name): string|null

Returns the header with the specified name.

This method is required by the SPL interface ArrayAccess. It is implicitly called when you use something like $header = $collection[$name];. This is equivalent to get().

Parameters

ParameterTypeDescription
$namestringThe header name

Returns

string|null: The header value with the specified name, null if the named header does not exist.

public function offsetSet(string $name, string $value): mixed

Adds the header to the collection.

This method is required by the SPL interface ArrayAccess. It is implicitly called when you use something like $collection[$name] = $header;. This is equivalent to add().

Parameters

ParameterTypeDescription
$namestringThe header name
$valuestringThe header value to be added

Returns

mixed

public function offsetUnset(string $name): mixed

Removes the named header.

This method is required by the SPL interface ArrayAccess. It is implicitly called when you use something like unset($collection[$name]). This is equivalent to remove().

Parameters

ParameterTypeDescription
$namestringThe header name

Returns

mixed

public function remove(string $name): array|null

Removes a header.

Parameters

ParameterTypeDescription
$namestringThe name of the header to be removed.

Returns

array|null: The value of the removed header. Null is returned if the header does not exist.

public function removeAll(): mixed

Removes all headers.

Returns

mixed

public function set(string $name, string $value = ''): $this

Adds a new header.

If there is already a header with the same name, it will be replaced.

Parameters

ParameterTypeDescription
$namestringThe name of the header
$valuestringThe value of the header

Returns

$this: The collection object itself

public function setDefault(string $name, string $value): $this

Sets a new header only if it does not exist yet.

If there is already a header with the same name, the new one will be ignored.

Parameters

ParameterTypeDescription
$namestringThe name of the header
$valuestringThe value of the header

Returns

$this: The collection object itself

public function toArray(): array

Returns the collection as a PHP array.

Returns

array: The array representation of the collection. The array keys are header names, and the array values are the corresponding header values.

public function toOriginalArray(): array

Returns the collection as a PHP array but instead of using normalized header names as keys (like toArray()) it uses original header names (case-sensitive).

Returns

array: The array representation of the collection.

Since

2.0.45