# CookieCollection

> CookieCollection maintains the cookies available in the current request.

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

| class | `yii\web\CookieCollection` |
| --- | --- |
| 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/CookieCollection.php`](https://github.com/yiisoft/yii2/blob/22.0/framework/web/CookieCollection.php#L31) |

CookieCollection maintains the cookies available in the current request.

For more details and usage information on CookieCollection, see the [guide article on handling cookies](/docs/guide/runtime-sessions-cookies/index.md).

## Properties

| Property | Type | Description | Defined by |
| --- | --- | --- | --- |
| <span id="property-count"></span>`$count` read-only | `int` | The number of cookies in the collection. | `CookieCollection` |
| <span id="property-iterator"></span>`$iterator` read-only | [`ArrayIterator`](https://www.php.net/manual/en/class.arrayiterator.php)`<string,` [`Cookie`](/docs/api/yii/web/cookie/index.md)`>` | An iterator for traversing the cookies in the collection. | `CookieCollection` |
| <span id="property-readOnly"></span>`$readOnly` | `bool` | Whether this collection is read only. Default: `false`. | `CookieCollection` |

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| [`__construct()`](#method-__construct) | Constructor. | `CookieCollection` |
| <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 cookie to the collection. | `CookieCollection` |
| <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 cookies in the collection. | `CookieCollection` |
| [`fromArray()`](#method-fromArray) | Populates the cookie collection from an array. | `CookieCollection` |
| [`get()`](#method-get) | Returns the cookie with the specified name. | `CookieCollection` |
| [`getCount()`](#method-getCount) | Returns the number of cookies in the collection. | `CookieCollection` |
| [`getIterator()`](#method-getIterator) | Returns an iterator for traversing the cookies in the collection. | `CookieCollection` |
| [`getValue()`](#method-getValue) | Returns the value of the named cookie. | `CookieCollection` |
| [`has()`](#method-has) | Returns whether there is a cookie with the specified name. | `CookieCollection` |
| <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 cookie with the specified name. | `CookieCollection` |
| [`offsetGet()`](#method-offsetGet) | Returns the cookie with the specified name. | `CookieCollection` |
| [`offsetSet()`](#method-offsetSet) | Adds the cookie to the collection. | `CookieCollection` |
| [`offsetUnset()`](#method-offsetUnset) | Removes the named cookie. | `CookieCollection` |
| [`remove()`](#method-remove) | Removes a cookie. | `CookieCollection` |
| [`removeAll()`](#method-removeAll) | Removes all cookies. | `CookieCollection` |
| [`toArray()`](#method-toArray) | Returns the collection as a PHP array. | `CookieCollection` |

## Method details

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

```php
public function __construct(array $cookies = [], array $config = [])
```

Constructor.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$cookies` | `array` | The cookies that this collection initially contains. This should be an array of name-value pairs. |
| `$config` | `array` | Name-value pairs that will be used to initialize the object properties |

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

```php
public function add(yii\web\Cookie $cookie): mixed
```

Adds a cookie to the collection.

If there is already a cookie with the same name in the collection, it will be removed first.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$cookie` | [`Cookie`](/docs/api/yii/web/cookie/index.md) | The cookie to be added |

**Returns**

`mixed`

**Throws**

- [`yii\base\InvalidCallException`](/docs/api/yii/base/invalidcallexception/index.md): if the cookie collection is read only

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

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

Returns the number of cookies 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 cookies in the collection.

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

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

Populates the cookie collection from an array.

**Parameters**

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

**Returns**

`mixed`

**Since**

2.0.3

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

```php
public function get(string $name): yii\web\Cookie|null
```

Returns the cookie with the specified name.

**Parameters**

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

**Returns**

[`Cookie`](/docs/api/yii/web/cookie/index.md)`|null`: The cookie with the specified name. Null if the named cookie does not exist.

**See also**

- [`getValue()`](/docs/api/yii/web/cookiecollection/index.md#method-getValue)

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

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

Returns the number of cookies in the collection.

**Returns**

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

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

```php
public function getIterator(): ArrayIterator<string, yii\web\Cookie>
```

Returns an iterator for traversing the cookies 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,` [`Cookie`](/docs/api/yii/web/cookie/index.md)`>`: An iterator for traversing the cookies in the collection.

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

```php
public function getValue(string $name, mixed $defaultValue = null): mixed
```

Returns the value of the named cookie.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$name` | `string` | The cookie name |
| `$defaultValue` | `mixed` | The value that should be returned when the named cookie does not exist. |

**Returns**

`mixed`: The value of the named cookie.

**See also**

- [`get()`](/docs/api/yii/web/cookiecollection/index.md#method-get)

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

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

Returns whether there is a cookie with the specified name.

Note that if a cookie is marked for deletion from browser or its value is an empty string, this method will return false.

**Parameters**

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

**Returns**

`bool`: Whether the named cookie exists

**See also**

- [`remove()`](/docs/api/yii/web/cookiecollection/index.md#method-remove)

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

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

Returns whether there is a cookie 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 cookie name |

**Returns**

`bool`: Whether the named cookie exists

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

```php
public function offsetGet(string $name): yii\web\Cookie|null
```

Returns the cookie 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 `$cookie = $collection[$name];`.
This is equivalent to [`get()`](/docs/api/yii/web/cookiecollection/index.md#method-get).

**Parameters**

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

**Returns**

[`Cookie`](/docs/api/yii/web/cookie/index.md)`|null`: The cookie with the specified name, null if the named cookie does not exist.

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

```php
public function offsetSet(string $name, yii\web\Cookie $cookie): mixed
```

Adds the cookie 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] = $cookie;`.
This is equivalent to [`add()`](/docs/api/yii/web/cookiecollection/index.md#method-add).

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$name` | `string` | The cookie name |
| `$cookie` | [`Cookie`](/docs/api/yii/web/cookie/index.md) | The cookie to be added |

**Returns**

`mixed`

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

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

Removes the named cookie.

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/cookiecollection/index.md#method-remove).

**Parameters**

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

**Returns**

`mixed`

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

```php
public function remove(yii\web\Cookie|string $cookie, bool $removeFromBrowser = true): mixed
```

Removes a cookie.

If `$removeFromBrowser` is true, the cookie will be removed from the browser.
In this case, a cookie with outdated expiry will be added to the collection.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$cookie` | [`Cookie`](/docs/api/yii/web/cookie/index.md)`\|string` | The cookie object or the name of the cookie to be removed. |
| `$removeFromBrowser` | `bool` | Whether to remove the cookie from browser |

**Returns**

`mixed`

**Throws**

- [`yii\base\InvalidCallException`](/docs/api/yii/base/invalidcallexception/index.md): if the cookie collection is read only

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

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

Removes all cookies.

**Returns**

`mixed`

**Throws**

- [`yii\base\InvalidCallException`](/docs/api/yii/base/invalidcallexception/index.md): if the cookie collection is read only

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

```php
public function toArray(): yii\web\Cookie[]
```

Returns the collection as a PHP array.

**Returns**

[`Cookie`](/docs/api/yii/web/cookie/index.md)`[]`: The array representation of the collection. The array keys are cookie names, and the array values are the corresponding cookie objects.