# HeaderCollection

> HeaderCollection is used by yii\web\Response to maintain the currently registered HTTP headers.

[API reference](/docs/api/index.md) › [yii](/docs/api/yii/index.md) › [web](/docs/api/yii/web/index.md) › HeaderCollection

| class | `yii\web\HeaderCollection` |
| --- | --- |
| Extends | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| Implements | [`IteratorAggregate`](https://www.php.net/manual/en/class.iteratoraggregate.php), [`ArrayAccess`](https://www.php.net/manual/en/class.arrayaccess.php), [`Countable`](https://www.php.net/manual/en/class.countable.php), [`yii\base\Configurable`](/docs/api/yii/base/configurable/index.md) |
| Since | 2.0 |
| Source | [`framework/web/HeaderCollection.php`](https://github.com/yiisoft/yii2/blob/22.0/framework/web/HeaderCollection.php#L26) |

HeaderCollection is used by [`Response`](/docs/api/yii/web/response/index.md) to maintain the currently registered HTTP headers.

## Properties

| Property | Type | Description | Defined by |
| --- | --- | --- | --- |
| <span id="property-count"></span>`$count` read-only | `int` | The number of headers in the collection. | `HeaderCollection` |
| <span id="property-iterator"></span>`$iterator` read-only | [`ArrayIterator`](https://www.php.net/manual/en/class.arrayiterator.php)`<string, string[]>` | An iterator for traversing the headers in the collection. | `HeaderCollection` |

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| <span id="method-__construct"></span>[`__construct()`](/docs/api/yii/base/baseobject/index.md#method-__construct) | Constructor. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__call"></span>[`__call()`](/docs/api/yii/base/baseobject/index.md#method-__call) | Calls the named method which is not a class method. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__get"></span>[`__get()`](/docs/api/yii/base/baseobject/index.md#method-__get) | Returns the value of an object property. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__isset"></span>[`__isset()`](/docs/api/yii/base/baseobject/index.md#method-__isset) | Checks if a property is set, i.e. defined and not null. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__set"></span>[`__set()`](/docs/api/yii/base/baseobject/index.md#method-__set) | Sets value of an object property. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__unset"></span>[`__unset()`](/docs/api/yii/base/baseobject/index.md#method-__unset) | Sets an object property to null. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| [`add()`](#method-add) | Adds a new header. | `HeaderCollection` |
| <span id="method-canGetProperty"></span>[`canGetProperty()`](/docs/api/yii/base/baseobject/index.md#method-canGetProperty) | Returns a value indicating whether a property can be read. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-canSetProperty"></span>[`canSetProperty()`](/docs/api/yii/base/baseobject/index.md#method-canSetProperty) | Returns a value indicating whether a property can be set. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-className"></span>[`className()`](/docs/api/yii/base/baseobject/index.md#method-className) static | Returns the fully qualified name of this class. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| [`count()`](#method-count) | Returns the number of headers in the collection. | `HeaderCollection` |
| [`fromArray()`](#method-fromArray) | Populates the header collection from an array. | `HeaderCollection` |
| [`get()`](#method-get) | Returns the named header(s). | `HeaderCollection` |
| [`getCount()`](#method-getCount) | Returns the number of headers in the collection. | `HeaderCollection` |
| [`getIterator()`](#method-getIterator) | Returns an iterator for traversing the headers in the collection. | `HeaderCollection` |
| [`has()`](#method-has) | Returns a value indicating whether the named header exists. | `HeaderCollection` |
| <span id="method-hasMethod"></span>[`hasMethod()`](/docs/api/yii/base/baseobject/index.md#method-hasMethod) | Returns a value indicating whether a method is defined. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-hasProperty"></span>[`hasProperty()`](/docs/api/yii/base/baseobject/index.md#method-hasProperty) | Returns a value indicating whether a property is defined. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-init"></span>[`init()`](/docs/api/yii/base/baseobject/index.md#method-init) | Initializes the object. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| [`offsetExists()`](#method-offsetExists) | Returns whether there is a header with the specified name. | `HeaderCollection` |
| [`offsetGet()`](#method-offsetGet) | Returns the header with the specified name. | `HeaderCollection` |
| [`offsetSet()`](#method-offsetSet) | Adds the header to the collection. | `HeaderCollection` |
| [`offsetUnset()`](#method-offsetUnset) | Removes the named header. | `HeaderCollection` |
| [`remove()`](#method-remove) | Removes a header. | `HeaderCollection` |
| [`removeAll()`](#method-removeAll) | Removes all headers. | `HeaderCollection` |
| [`set()`](#method-set) | Adds a new header. | `HeaderCollection` |
| [`setDefault()`](#method-setDefault) | Sets a new header only if it does not exist yet. | `HeaderCollection` |
| [`toArray()`](#method-toArray) | Returns the collection as a PHP array. | `HeaderCollection` |
| [`toOriginalArray()`](#method-toOriginalArray) | Returns the collection as a PHP array but instead of using normalized header names as keys (like [`toArray()`](/docs/api/yii/web/headercollection/index.md#method-toArray)) it uses original header names (case-sensitive). | `HeaderCollection` |

## Method details

### add() <span id="method-add"></span>

```php
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**

| Parameter | Type | Description |
| --- | --- | --- |
| `$name` | `string` | The name of the header |
| `$value` | `string` | The value of the header |

**Returns**

`$this`: The collection object itself

### count() <span id="method-count"></span>

```php
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.

### fromArray() <span id="method-fromArray"></span>

```php
public function fromArray(array $array): mixed
```

Populates the header collection from an array.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$array` | `array` | The headers to populate from |

**Returns**

`mixed`

**Since**

2.0.3

### get() <span id="method-get"></span>

```php
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\|null : string[]\|null)` | 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() <span id="method-getCount"></span>

```php
public function getCount(): int
```

Returns the number of headers in the collection.

**Returns**

`int`: The number of headers in the collection.

### getIterator() <span id="method-getIterator"></span>

```php
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`](https://www.php.net/manual/en/class.iteratoraggregate.php).
It will be implicitly called when you use `foreach` to traverse the collection.

**Returns**

[`ArrayIterator`](https://www.php.net/manual/en/class.arrayiterator.php)`<string, string[]>`: An iterator for traversing the headers in the collection.

### has() <span id="method-has"></span>

```php
public function has(string $name): bool
```

Returns 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() <span id="method-offsetExists"></span>

```php
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`](https://www.php.net/manual/en/class.arrayaccess.php).
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() <span id="method-offsetGet"></span>

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

Returns the header with the specified name.

This method is required by the SPL interface [`ArrayAccess`](https://www.php.net/manual/en/class.arrayaccess.php).
It is implicitly called when you use something like `$header = $collection[$name];`.
This is equivalent to [`get()`](/docs/api/yii/web/headercollection/index.md#method-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() <span id="method-offsetSet"></span>

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

Adds the header to the collection.

This method is required by the SPL interface [`ArrayAccess`](https://www.php.net/manual/en/class.arrayaccess.php).
It is implicitly called when you use something like `$collection[$name] = $header;`.
This is equivalent to [`add()`](/docs/api/yii/web/headercollection/index.md#method-add).

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$name` | `string` | The header name |
| `$value` | `string` | The header value to be added |

**Returns**

`mixed`

### offsetUnset() <span id="method-offsetUnset"></span>

```php
public function offsetUnset(string $name): mixed
```

Removes the named header.

This method is required by the SPL interface [`ArrayAccess`](https://www.php.net/manual/en/class.arrayaccess.php).
It is implicitly called when you use something like `unset($collection[$name])`.
This is equivalent to [`remove()`](/docs/api/yii/web/headercollection/index.md#method-remove).

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$name` | `string` | The header name |

**Returns**

`mixed`

### remove() <span id="method-remove"></span>

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

Removes 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() <span id="method-removeAll"></span>

```php
public function removeAll(): mixed
```

Removes all headers.

**Returns**

`mixed`

### set() <span id="method-set"></span>

```php
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**

| Parameter | Type | Description |
| --- | --- | --- |
| `$name` | `string` | The name of the header |
| `$value` | `string` | The value of the header |

**Returns**

`$this`: The collection object itself

### setDefault() <span id="method-setDefault"></span>

```php
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**

| Parameter | Type | Description |
| --- | --- | --- |
| `$name` | `string` | The name of the header |
| `$value` | `string` | The value of the header |

**Returns**

`$this`: The collection object itself

### toArray() <span id="method-toArray"></span>

```php
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.

### toOriginalArray() <span id="method-toOriginalArray"></span>

```php
public function toOriginalArray(): array
```

Returns the collection as a PHP array but instead of using normalized header names as keys (like [`toArray()`](/docs/api/yii/web/headercollection/index.md#method-toArray))
it uses original header names (case-sensitive).

**Returns**

`array`: The array representation of the collection.

**Since**

2.0.45