Class yii\web\CookieCollection

Inheritanceyii\web\CookieCollection » yii\base\BaseObject
ImplementsArrayAccess, Countable, IteratorAggregate, yii\base\Configurable
Available since version2.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. yii\web\CookieCollection
$iterator \ArrayIterator An iterator for traversing the cookies in the collection. 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

Hide inherited properties

$count public property

The number of cookies in the collection.

public integer $count null
$iterator public read-only property

An iterator for traversing the cookies in the collection.

public \ArrayIterator getIterator ( )
$readOnly public property

Whether this collection is read only.

public boolean $readOnly false

Method Details

Hide inherited methods

__call() public method

Defined in: yii\base\BaseObject::__call()

Calls the named method which is not a class method.

Do not call this method directly as it is a PHP magic method that will be implicitly called when an unknown method is being invoked.

public mixed __call ( $name, $params )
$name string

The method name

$params array

Method parameters

return mixed

The method return value

throws yii\base\UnknownMethodException

when calling unknown method

                public function __call($name, $params)
{
    throw new UnknownMethodException('Calling unknown method: ' . get_class($this) . "::$name()");
}

            
__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

                public function __construct($cookies = [], $config = [])
{
    $this->_cookies = $cookies;
    parent::__construct($config);
}

            
__get() public method

Defined in: yii\base\BaseObject::__get()

Returns the value of an object property.

Do not call this method directly as it is a PHP magic method that will be implicitly called when executing $value = $object->property;.

See also __set().

public mixed __get ( $name )
$name string

The property name

return mixed

The property value

throws yii\base\UnknownPropertyException

if the property is not defined

throws yii\base\InvalidCallException

if the property is write-only

                public function __get($name)
{
    $getter = 'get' . $name;
    if (method_exists($this, $getter)) {
        return $this->$getter();
    } elseif (method_exists($this, 'set' . $name)) {
        throw new InvalidCallException('Getting write-only property: ' . get_class($this) . '::' . $name);
    }
    throw new UnknownPropertyException('Getting unknown property: ' . get_class($this) . '::' . $name);
}

            
__isset() public method

Defined in: yii\base\BaseObject::__isset()

Checks if a property is set, i.e. defined and not null.

Do not call this method directly as it is a PHP magic method that will be implicitly called when executing isset($object->property).

Note that if the property is not defined, false will be returned.

See also https://www.php.net/manual/en/function.isset.php.

public boolean __isset ( $name )
$name string

The property name or the event name

return boolean

Whether the named property is set (not null).

                public function __isset($name)
{
    $getter = 'get' . $name;
    if (method_exists($this, $getter)) {
        return $this->$getter() !== null;
    }
    return false;
}

            
__set() public method

Defined in: yii\base\BaseObject::__set()

Sets value of an object property.

Do not call this method directly as it is a PHP magic method that will be implicitly called when executing $object->property = $value;.

See also __get().

public void __set ( $name, $value )
$name string

The property name or the event name

$value mixed

The property value

throws yii\base\UnknownPropertyException

if the property is not defined

throws yii\base\InvalidCallException

if the property is read-only

                public function __set($name, $value)
{
    $setter = 'set' . $name;
    if (method_exists($this, $setter)) {
        $this->$setter($value);
    } elseif (method_exists($this, 'get' . $name)) {
        throw new InvalidCallException('Setting read-only property: ' . get_class($this) . '::' . $name);
    } else {
        throw new UnknownPropertyException('Setting unknown property: ' . get_class($this) . '::' . $name);
    }
}

            
__unset() public method

Defined in: yii\base\BaseObject::__unset()

Sets an object property to null.

Do not call this method directly as it is a PHP magic method that will be implicitly called when executing unset($object->property).

Note that if the property is not defined, this method will do nothing. If the property is read-only, it will throw an exception.

See also https://www.php.net/manual/en/function.unset.php.

public void __unset ( $name )
$name string

The property name

throws yii\base\InvalidCallException

if the property is read only.

                public function __unset($name)
{
    $setter = 'set' . $name;
    if (method_exists($this, $setter)) {
        $this->$setter(null);
    } elseif (method_exists($this, 'get' . $name)) {
        throw new InvalidCallException('Unsetting read-only property: ' . get_class($this) . '::' . $name);
    }
}

            
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

                public function add($cookie)
{
    if ($this->readOnly) {
        throw new InvalidCallException('The cookie collection is read only.');
    }
    $this->_cookies[$cookie->name] = $cookie;
}

            
canGetProperty() public method

Defined in: yii\base\BaseObject::canGetProperty()

Returns a value indicating whether a property can be read.

A property is readable if:

  • the class has a getter method associated with the specified name (in this case, property name is case-insensitive);
  • the class has a member variable with the specified name (when $checkVars is true);

See also canSetProperty().

public boolean canGetProperty ( $name, $checkVars true )
$name string

The property name

$checkVars boolean

Whether to treat member variables as properties

return boolean

Whether the property can be read

                public function canGetProperty($name, $checkVars = true)
{
    return method_exists($this, 'get' . $name) || $checkVars && property_exists($this, $name);
}

            
canSetProperty() public method

Defined in: yii\base\BaseObject::canSetProperty()

Returns a value indicating whether a property can be set.

A property is writable if:

  • the class has a setter method associated with the specified name (in this case, property name is case-insensitive);
  • the class has a member variable with the specified name (when $checkVars is true);

See also canGetProperty().

public boolean canSetProperty ( $name, $checkVars true )
$name string

The property name

$checkVars boolean

Whether to treat member variables as properties

return boolean

Whether the property can be written

                public function canSetProperty($name, $checkVars = true)
{
    return method_exists($this, 'set' . $name) || $checkVars && property_exists($this, $name);
}

            
className() public static method
Deprecated since 2.0.14. On PHP >=5.5, use ::class instead.

Defined in: yii\base\BaseObject::className()

Returns the fully qualified name of this class.

public static string className ( )
return string

The fully qualified name of this class.

                public static function className()
{
    return get_called_class();
}

            
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.

                #[\ReturnTypeWillChange]
public function count()
{
    return $this->getCount();
}

            
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

                public function fromArray(array $array)
{
    $this->_cookies = $array;
}

            
get() public method

Returns the cookie with the specified name.

See also getValue().

public yii\web\Cookie|null get ( $name )
$name string

The cookie name

return yii\web\Cookie|null

The cookie with the specified name. Null if the named cookie does not exist.

                public function get($name)
{
    return isset($this->_cookies[$name]) ? $this->_cookies[$name] : null;
}

            
getCount() public method

Returns the number of cookies in the collection.

public integer getCount ( )
return integer

The number of cookies in the collection.

                public function getCount()
{
    return count($this->_cookies);
}

            
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.

                #[\ReturnTypeWillChange]
public function getIterator()
{
    return new ArrayIterator($this->_cookies);
}

            
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.

                public function getValue($name, $defaultValue = null)
{
    return isset($this->_cookies[$name]) ? $this->_cookies[$name]->value : $defaultValue;
}

            
has() public method

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.

See also remove().

public boolean has ( $name )
$name string

The cookie name

return boolean

Whether the named cookie exists

                public function has($name)
{
    return isset($this->_cookies[$name]) && $this->_cookies[$name]->value !== ''
        && ($this->_cookies[$name]->expire === null
            || $this->_cookies[$name]->expire === 0
            || (
                (is_string($this->_cookies[$name]->expire) && strtotime($this->_cookies[$name]->expire) >= time())
                || (
                    interface_exists('\\DateTimeInterface')
                    && $this->_cookies[$name]->expire instanceof \DateTimeInterface
                    && $this->_cookies[$name]->expire->getTimestamp() >= time()
                )
                || $this->_cookies[$name]->expire >= time()
            )
        );
}

            
hasMethod() public method

Defined in: yii\base\BaseObject::hasMethod()

Returns a value indicating whether a method is defined.

The default implementation is a call to php function method_exists(). You may override this method when you implemented the php magic method __call().

public boolean hasMethod ( $name )
$name string

The method name

return boolean

Whether the method is defined

                public function hasMethod($name)
{
    return method_exists($this, $name);
}

            
hasProperty() public method

Defined in: yii\base\BaseObject::hasProperty()

Returns a value indicating whether a property is defined.

A property is defined if:

  • the class has a getter or setter method associated with the specified name (in this case, property name is case-insensitive);
  • the class has a member variable with the specified name (when $checkVars is true);

See also:

public boolean hasProperty ( $name, $checkVars true )
$name string

The property name

$checkVars boolean

Whether to treat member variables as properties

return boolean

Whether the property is defined

                public function hasProperty($name, $checkVars = true)
{
    return $this->canGetProperty($name, $checkVars) || $this->canSetProperty($name, false);
}

            
init() public method

Defined in: yii\base\BaseObject::init()

Initializes the object.

This method is invoked at the end of the constructor after the object is initialized with the given configuration.

public void init ( )

                public function init()
{
}

            
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

                #[\ReturnTypeWillChange]
public function offsetExists($name)
{
    return $this->has($name);
}

            
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|null offsetGet ( $name )
$name string

The cookie name

return yii\web\Cookie|null

The cookie with the specified name, null if the named cookie does not exist.

                #[\ReturnTypeWillChange]
public function offsetGet($name)
{
    return $this->get($name);
}

            
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

                #[\ReturnTypeWillChange]
public function offsetSet($name, $cookie)
{
    $this->add($cookie);
}

            
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

                #[\ReturnTypeWillChange]
public function offsetUnset($name)
{
    $this->remove($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

                public function remove($cookie, $removeFromBrowser = true)
{
    if ($this->readOnly) {
        throw new InvalidCallException('The cookie collection is read only.');
    }
    if ($cookie instanceof Cookie) {
        $cookie->expire = 1;
        $cookie->value = '';
    } else {
        $cookie = Yii::createObject([
            'class' => 'yii\web\Cookie',
            'name' => $cookie,
            'expire' => 1,
        ]);
    }
    if ($removeFromBrowser) {
        $this->_cookies[$cookie->name] = $cookie;
    } else {
        unset($this->_cookies[$cookie->name]);
    }
}

            
removeAll() public method

Removes all cookies.

public void removeAll ( )
throws yii\base\InvalidCallException

if the cookie collection is read only

                public function removeAll()
{
    if ($this->readOnly) {
        throw new InvalidCallException('The cookie collection is read only.');
    }
    $this->_cookies = [];
}

            
toArray() public method

Returns the collection as a PHP array.

public yii\web\Cookie[] toArray ( )
return yii\web\Cookie[]

The array representation of the collection. The array keys are cookie names, and the array values are the corresponding cookie objects.

                public function toArray()
{
    return $this->_cookies;
}