# Class yii\web\CookieCollection

Inheritance | yii\web\CookieCollection » yii\base\BaseObject
Implements | ArrayAccess, Countable, IteratorAggregate, yii\base\Configurable
Available since version | 2.0
Source Code | https://github.com/yiisoft/yii2/blob/master/framework/web/CookieCollection.php

CookieCollection maintains the cookies available in the current request.

For more details and usage information on CookieCollection, see the guide article on handling cookies.

## Public Properties

Hide inherited properties

Property | Type | Description | Defined By
--- | --- | --- | ---
$count | integer | The number of cookies in the collection. This property is read-only. | yii\web\CookieCollection
$iterator | ArrayIterator | An iterator for traversing the cookies in the collection. This property is read-only. | yii\web\CookieCollection
$readOnly | boolean | Whether this collection is read only. | yii\web\CookieCollection

## Public Methods

Hide inherited methods

Method | Description | Defined By
--- | --- | ---
__call() | Calls the named method which is not a class method. | yii\base\BaseObject
__construct() | Constructor. | yii\web\CookieCollection
__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 cookie to the collection. | yii\web\CookieCollection
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() | Returns the fully qualified name of this class. | yii\base\BaseObject
count() | Returns the number of cookies in the collection. | yii\web\CookieCollection
fromArray() | Populates the cookie collection from an array. | yii\web\CookieCollection
get() | Returns the cookie with the specified name. | yii\web\CookieCollection
getCount() | Returns the number of cookies in the collection. | yii\web\CookieCollection
getIterator() | Returns an iterator for traversing the cookies in the collection. | yii\web\CookieCollection
getValue() | Returns the value of the named cookie. | yii\web\CookieCollection
has() | Returns whether there is a cookie with the specified name. | yii\web\CookieCollection
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 cookie with the specified name. | yii\web\CookieCollection
offsetGet() | Returns the cookie with the specified name. | yii\web\CookieCollection
offsetSet() | Adds the cookie to the collection. | yii\web\CookieCollection
offsetUnset() | Removes the named cookie. | yii\web\CookieCollection
remove() | Removes a cookie. | yii\web\CookieCollection
removeAll() | Removes all cookies. | yii\web\CookieCollection
toArray() | Returns the collection as a PHP array. | yii\web\CookieCollection

## Property Details

$count public property

The number of cookies in the collection. This property is read-only.

public integer $count = null
$iterator public property

An iterator for traversing the cookies in the collection. This property is read-only.

public ArrayIterator $iterator = null
$readOnly public property

Whether this collection is read only.

public boolean $readOnly = false

## Method Details

__construct() public method

Constructor.

public void __construct ( $cookies = [], $config = [] )
$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() public method

Adds a cookie to the collection.

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

public void add ( $cookie )
$cookie | yii\web\Cookie | The cookie to be added
throws | yii\base\InvalidCallException | if the cookie collection is read only

count() public method

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

public integer count ( )
return | integer | The number of cookies in the collection.

fromArray() public method (available since version 2.0.3)

Populates the cookie collection from an array.

public void fromArray ( array $array )
$array | array | The cookies to populate from

get() public method

Returns the cookie with the specified name.

See also getValue().

public yii\web\Cookie get ( $name )
$name | string | The cookie name
return | yii\web\Cookie | The cookie with the specified name. Null if the named cookie does not exist.

getCount() public method

Returns the number of cookies in the collection.

public integer getCount ( )
return | integer | The number of cookies in the collection.

getIterator() public method

Returns an iterator for traversing the cookies 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.

public ArrayIterator getIterator ( )
return | ArrayIterator | An iterator for traversing the cookies in the collection.

getValue() public method

Returns the value of the named cookie.

See also get().

public mixed getValue ( $name, $defaultValue = null )
$name | string | The cookie name
$defaultValue | mixed | The value that should be returned when the named cookie does not exist.
return | mixed | The value of the named cookie.

has() public method

Returns whether there is a cookie with the specified name.

Note that if a cookie is marked for deletion from browser, this method will return false.

See also remove().

public boolean has ( $name )
$name | string | The cookie name
return | boolean | Whether the named cookie exists

offsetExists() public method

Returns whether there is a cookie 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])`.

public boolean offsetExists ( $name )
$name | string | The cookie name
return | boolean | Whether the named cookie exists

offsetGet() public method

Returns the cookie with the specified name.

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

public yii\web\Cookie offsetGet ( $name )
$name | string | The cookie name
return | yii\web\Cookie | The cookie with the specified name, null if the named cookie does not exist.

offsetSet() public method

Adds the cookie to the collection.

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

public void offsetSet ( $name, $cookie )
$name | string | The cookie name
$cookie | yii\web\Cookie | The cookie to be added

offsetUnset() public method

Removes the named cookie.

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().

public void offsetUnset ( $name )
$name | string | The cookie name

remove() public method

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.

public void remove ( $cookie, $removeFromBrowser = true )
$cookie | yii\web\Cookie|string | The cookie object or the name of the cookie to be removed.
$removeFromBrowser | boolean | Whether to remove the cookie from browser
throws | yii\base\InvalidCallException | if the cookie collection is read only

removeAll() public method

Removes all cookies.

public void removeAll ( )
throws | yii\base\InvalidCallException | if the cookie collection is read only

toArray() public method

Returns the collection as a PHP array.

public array toArray ( )
return | array | The array representation of the collection. The array keys are cookie names, and the array values are the corresponding cookie objects.