HeaderCollection
API reference › yii › web › HeaderCollection
HeaderCollection is used by Response to maintain the currently registered HTTP headers.
Properties
Section titled “Properties”| Property | Type | Description | Defined by |
|---|---|---|---|
$count read-only | int | The number of headers in the collection. | HeaderCollection |
$iterator read-only | ArrayIterator<string, | An iterator for traversing the headers in the collection. | HeaderCollection |
Methods
Section titled “Methods”| Method | Description | Defined by |
|---|---|---|
__construct() | Constructor. | yii\ |
__call() | Calls the named method which is not a class method. | yii\ |
__get() | Returns the value of an object property. | yii\ |
__isset() | Checks if a property is set, i.e. defined and not null. | yii\ |
__set() | Sets value of an object property. | yii\ |
__unset() | Sets an object property to null. | yii\ |
add() | Adds a new header. | HeaderCollection |
canGetProperty() | Returns a value indicating whether a property can be read. | yii\ |
canSetProperty() | Returns a value indicating whether a property can be set. | yii\ |
className() static | Returns the fully qualified name of this class. | yii\ |
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\ |
hasProperty() | Returns a value indicating whether a property is defined. | yii\ |
init() | Initializes the object. | yii\ |
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 |
Method details
Section titled “Method details”add()
Section titled “add() ”public function add(string $name, string $value): $thisAdds 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
| Parameter | Type | Description |
|---|---|---|
$name | string | The name of the header |
$value | string | The value of the header |
Returns
$this: The collection object itself
count()
Section titled “count() ”public function count(): intReturns 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.
fromArray()
Section titled “fromArray() ”public function fromArray(array $array): mixedPopulates the header collection from an array.
Parameters
| Parameter | Type | Description |
|---|---|---|
$array | array | The headers to populate from |
Returns
mixed
Since
2.0.3
get()
Section titled “get() ”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
| Parameter | Type | Description |
|---|---|---|
$name | string | The name of the header to return |
$default | ($first is true ? string| | The value to return in case the named header does not exist |
$first | bool | Whether 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.
getCount()
Section titled “getCount() ”public function getCount(): intReturns the number of headers in the collection.
Returns
int: The number of headers in the collection.
getIterator()
Section titled “getIterator() ”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.
has()
Section titled “has() ”public function has(string $name): boolReturns a value indicating whether the named header exists.
Parameters
| Parameter | Type | Description |
|---|---|---|
$name | string | The name of the header |
Returns
bool: Whether the named header exists
offsetExists()
Section titled “offsetExists() ”public function offsetExists(string $name): boolReturns 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
| Parameter | Type | Description |
|---|---|---|
$name | string | The header name |
Returns
bool: Whether the named header exists
offsetGet()
Section titled “offsetGet() ”public function offsetGet(string $name): string|nullReturns 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
| Parameter | Type | Description |
|---|---|---|
$name | string | The header name |
Returns
string|null: The header value with the specified name, null if the named header does not exist.
offsetSet()
Section titled “offsetSet() ”public function offsetSet(string $name, string $value): mixedAdds 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
| Parameter | Type | Description |
|---|---|---|
$name | string | The header name |
$value | string | The header value to be added |
Returns
mixed
offsetUnset()
Section titled “offsetUnset() ”public function offsetUnset(string $name): mixedRemoves 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
| Parameter | Type | Description |
|---|---|---|
$name | string | The header name |
Returns
mixed
remove()
Section titled “remove() ”public function remove(string $name): array|nullRemoves a header.
Parameters
| Parameter | Type | Description |
|---|---|---|
$name | string | The 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.
removeAll()
Section titled “removeAll() ”public function removeAll(): mixedRemoves all headers.
Returns
mixed
set()
Section titled “set() ”public function set(string $name, string $value = ''): $thisAdds a new header.
If there is already a header with the same name, it will be replaced.
Parameters
| Parameter | Type | Description |
|---|---|---|
$name | string | The name of the header |
$value | string | The value of the header |
Returns
$this: The collection object itself
setDefault()
Section titled “setDefault() ”public function setDefault(string $name, string $value): $thisSets 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
| Parameter | Type | Description |
|---|---|---|
$name | string | The name of the header |
$value | string | The value of the header |
Returns
$this: The collection object itself
toArray()
Section titled “toArray() ”public function toArray(): arrayReturns 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.
toOriginalArray()
Section titled “toOriginalArray() ”public function toOriginalArray(): arrayReturns 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