PHP Class yii\web\CookieCollection

For more details and usage information on CookieCollection, see the guide article on handling cookies.
Since: 2.0
Author: Qiang Xue ([email protected])
Inheritance: extends yii\base\Object, implements IteratorAggregate, implements ArrayAccess, implements Countable
Show file Open project: yiisoft/yii2 Class Usage Examples

Public Properties

Property Type Description
$readOnly whether this collection is read only.

Public Methods

Method Description
__construct ( array $cookies = [], array $config = [] ) Constructor.
add ( Cookie $cookie ) Adds a cookie to the collection.
count ( ) : integer Returns the number of cookies in the collection.
fromArray ( array $array ) Populates the cookie collection from an array.
get ( string $name ) : Cookie Returns the cookie with the specified name.
getCount ( ) : integer Returns the number of cookies in the collection.
getIterator ( ) : ArrayIterator Returns an iterator for traversing the cookies in the collection.
getValue ( string $name, mixed $defaultValue = null ) : mixed Returns the value of the named cookie.
has ( string $name ) : boolean Returns whether there is a cookie with the specified name.
offsetExists ( string $name ) : boolean Returns whether there is a cookie with the specified name.
offsetGet ( string $name ) : Cookie Returns the cookie with the specified name.
offsetSet ( string $name, Cookie $cookie ) Adds the cookie to the collection.
offsetUnset ( string $name ) Removes the named cookie.
remove ( Cookie | string $cookie, boolean $removeFromBrowser = true ) Removes a cookie.
removeAll ( ) Removes all cookies.
toArray ( ) : array Returns the collection as a PHP array.

Method Details

__construct() public method

Constructor.
public __construct ( array $cookies = [], array $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

If there is already a cookie with the same name in the collection, it will be removed first.
public add ( Cookie $cookie )
$cookie Cookie the cookie to be added

count() public method

This method is required by the SPL Countable interface. It will be implicitly called when you use count($collection).
public count ( ) : integer
return integer the number of cookies in the collection.

fromArray() public method

Populates the cookie collection from an array.
Since: 2.0.3
public fromArray ( array $array )
$array array the cookies to populate from

get() public method

Returns the cookie with the specified name.
See also: getValue()
public get ( string $name ) : Cookie
$name string the cookie name
return 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 getCount ( ) : integer
return integer the number of cookies in the collection.

getIterator() public method

This method is required by the SPL interface [[\IteratorAggregate]]. It will be implicitly called when you use foreach to traverse the collection.
public getIterator ( ) : ArrayIterator
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 getValue ( string $name, mixed $defaultValue = null ) : mixed
$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

Note that if a cookie is marked for deletion from browser, this method will return false.
See also: remove()
public has ( string $name ) : boolean
$name string the cookie name
return boolean whether the named cookie exists

offsetExists() public method

This method is required by the SPL interface [[\ArrayAccess]]. It is implicitly called when you use something like isset($collection[$name]).
public offsetExists ( string $name ) : boolean
$name string the cookie name
return boolean whether the named cookie exists

offsetGet() public method

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 CookieCollection::get.
public offsetGet ( string $name ) : Cookie
$name string the cookie name
return Cookie the cookie with the specified name, null if the named cookie does not exist.

offsetSet() public method

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 CookieCollection::add.
public offsetSet ( string $name, Cookie $cookie )
$name string the cookie name
$cookie Cookie the cookie to be added

offsetUnset() public method

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 CookieCollection::remove.
public offsetUnset ( string $name )
$name string the cookie name

remove() public method

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 remove ( Cookie | string $cookie, boolean $removeFromBrowser = true )
$cookie Cookie | string the cookie object or the name of the cookie to be removed.
$removeFromBrowser boolean whether to remove the cookie from browser

removeAll() public method

Removes all cookies.
public removeAll ( )

toArray() public method

Returns the collection as a PHP array.
public toArray ( ) : array
return array the array representation of the collection. The array keys are cookie names, and the array values are the corresponding cookie objects.

Property Details

$readOnly public property

whether this collection is read only.
public $readOnly