0 追蹤者

類別 yii\web\CookieCollection

繼承關係yii\web\CookieCollection » yii\base\BaseObject
實作介面ArrayAccessCountableIteratorAggregateyii\base\Configurable
自版本起可用2.0
原始碼 https://github.com/yiisoft/yii2/blob/master/framework/web/CookieCollection.php

CookieCollection 維護目前請求中可用的 Cookie。

關於 CookieCollection 的更多詳細資訊和使用方法,請參閱處理 Cookie 的指南文章

公開屬性

隱藏繼承的屬性

屬性 類型 描述 定義於
$count integer 集合中 Cookie 的數量。 yii\web\CookieCollection
$iterator \ArrayIterator 用於遍歷集合中 Cookie 的迭代器。 yii\web\CookieCollection
$readOnly boolean 此集合是否為唯讀。 yii\web\CookieCollection

公開方法

隱藏繼承的方法

方法 描述 定義於
__call() 調用非類別方法的具名方法。 yii\base\BaseObject
__construct() 建構子。 yii\web\CookieCollection
__get() 傳回物件屬性的值。 yii\base\BaseObject
__isset() 檢查屬性是否已設定,即已定義且非 null。 yii\base\BaseObject
__set() 設定物件屬性的值。 yii\base\BaseObject
__unset() 將物件屬性設定為 null。 yii\base\BaseObject
add() 向集合中新增一個 Cookie。 yii\web\CookieCollection
canGetProperty() 傳回一個值,指示屬性是否可讀。 yii\base\BaseObject
canSetProperty() 傳回一個值,指示屬性是否可設定。 yii\base\BaseObject
className() 傳回此類別的完整限定名稱。 yii\base\BaseObject
count() 傳回集合中 Cookie 的數量。 yii\web\CookieCollection
fromArray() 從陣列填充 Cookie 集合。 yii\web\CookieCollection
get() 傳回具有指定名稱的 Cookie。 yii\web\CookieCollection
getCount() 傳回集合中 Cookie 的數量。 yii\web\CookieCollection
getIterator() 傳回用於遍歷集合中 Cookie 的迭代器。 yii\web\CookieCollection
getValue() 傳回具名 Cookie 的值。 yii\web\CookieCollection
has() 傳回是否有名稱指定的 Cookie。 yii\web\CookieCollection
hasMethod() 傳回一個值,指示是否已定義方法。 yii\base\BaseObject
hasProperty() 傳回一個值,指示是否已定義屬性。 yii\base\BaseObject
init() 初始化物件。 yii\base\BaseObject
offsetExists() 傳回是否有名稱指定的 Cookie。 yii\web\CookieCollection
offsetGet() 傳回具有指定名稱的 Cookie。 yii\web\CookieCollection
offsetSet() 將 Cookie 新增至集合。 yii\web\CookieCollection
offsetUnset() 移除具名 Cookie。 yii\web\CookieCollection
remove() 移除一個 Cookie。 yii\web\CookieCollection
removeAll() 移除所有 Cookie。 yii\web\CookieCollection
toArray() 以 PHP 陣列形式傳回集合。 yii\web\CookieCollection

屬性詳細資訊

隱藏繼承的屬性

$count 公開屬性

集合中 Cookie 的數量。

public integer $count null
$iterator 公開唯讀屬性

用於遍歷集合中 Cookie 的迭代器。

public \ArrayIterator getIterator ( )
$readOnly 公開屬性

此集合是否為唯讀。

public boolean $readOnly false

方法詳細資訊

隱藏繼承的方法

__call() public method

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

調用非類別方法的具名方法。

請勿直接呼叫此方法,因為它是 PHP 的魔術方法,當調用未知方法時會被隱式地呼叫。

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

方法名稱

$params array

方法參數

return mixed

方法回傳值

throws yii\base\UnknownMethodException

當調用未知方法時

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

            
__construct() public method

建構子。

public void __construct ( $cookies = [], $config = [] )
$cookies array

此集合初始包含的 Cookie。這應該是一個名稱-值對的陣列。

$config array

將用於初始化物件屬性的名稱-值對

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

            
__get() public method

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

傳回物件屬性的值。

請勿直接呼叫此方法,因為它是 PHP 的魔術方法,當執行 $value = $object->property; 時會被隱式地呼叫。

參見 __set()

public mixed __get ( $name )
$name string

屬性名稱

return mixed

屬性值

throws yii\base\UnknownPropertyException

如果屬性未定義

throws yii\base\InvalidCallException

如果屬性為唯寫

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

檢查屬性是否已設定,即已定義且非 null。

請勿直接呼叫此方法,因為它是 PHP 的魔術方法,當執行 isset($object->property) 時會被隱式地呼叫。

請注意,如果屬性未定義,將會回傳 false。

參見 https://php.dev.org.tw/manual/en/function.isset.php

public boolean __isset ( $name )
$name string

屬性名稱或事件名稱

return boolean

具名屬性是否已設定(非 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()

設定物件屬性的值。

請勿直接呼叫此方法,因為它是 PHP 的魔術方法,當執行 $object->property = $value; 時會被隱式地呼叫。

參見 __get()

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

屬性名稱或事件名稱

$value mixed

屬性值

throws yii\base\UnknownPropertyException

如果屬性未定義

throws yii\base\InvalidCallException

如果屬性為唯讀

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

將物件屬性設定為 null。

請勿直接呼叫此方法,因為它是 PHP 的魔術方法,當執行 unset($object->property) 時會被隱式地呼叫。

請注意,如果屬性未定義,此方法將不會執行任何操作。如果屬性為唯讀,則會拋出例外。

參見 https://php.dev.org.tw/manual/en/function.unset.php

public void __unset ( $name )
$name string

屬性名稱

throws yii\base\InvalidCallException

如果屬性為唯讀。

                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

向集合中新增一個 Cookie。

如果集合中已存在同名的 Cookie,它會先被移除。

public void add ( $cookie )
$cookie yii\web\Cookie

要加入的 Cookie

throws yii\base\InvalidCallException

如果 Cookie 集合為唯讀

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

傳回一個值,指示屬性是否可讀。

如果符合以下條件,則屬性為可讀:

  • 類別具有與指定名稱關聯的 getter 方法(在這種情況下,屬性名稱不區分大小寫);
  • 類別具有帶有指定名稱的成員變數(當 $checkVars 為 true 時);

參見 canSetProperty()

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

屬性名稱

$checkVars boolean

是否將成員變數視為屬性

return boolean

屬性是否可讀

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

傳回一個值,指示屬性是否可設定。

如果符合以下條件,則屬性為可寫:

  • 類別具有與指定名稱關聯的 setter 方法(在這種情況下,屬性名稱不區分大小寫);
  • 類別具有帶有指定名稱的成員變數(當 $checkVars 為 true 時);

參見 canGetProperty()

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

屬性名稱

$checkVars boolean

是否將成員變數視為屬性

return boolean

屬性是否可寫

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

            
className() public static method
自 2.0.14 版本起已過時。在 PHP >=5.5 上,請改用 ::class

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

傳回此類別的完整限定名稱。

public static string className ( )
return string

此類別的完整限定名稱。

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

            
count() public method

傳回集合中 Cookie 的數量。

SPL Countable 介面需要此方法。當您使用 count($collection) 時,它會被隱式地呼叫。

public integer count ( )
return integer

集合中 Cookie 的數量。

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

            
fromArray() public method (available since version 2.0.3)

從陣列填充 Cookie 集合。

public void fromArray ( array $array )
$array array

要從中填充的 Cookie

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

            
get() public method

傳回具有指定名稱的 Cookie。

參見 getValue()

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

Cookie 名稱

return yii\web\Cookie|null

具有指定名稱的 Cookie。如果具名 Cookie 不存在,則為 Null。

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

            
getCount() public method

傳回集合中 Cookie 的數量。

public integer getCount ( )
return integer

集合中 Cookie 的數量。

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

            
getIterator() public method

傳回用於遍歷集合中 Cookie 的迭代器。

SPL 介面 IteratorAggregate 需要此方法。當您使用 foreach 遍歷集合時,它會被隱式地呼叫。

public \ArrayIterator getIterator ( )
return \ArrayIterator

用於遍歷集合中 Cookie 的迭代器。

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

            
getValue() public method

傳回具名 Cookie 的值。

參見 get()

public mixed getValue ( $name, $defaultValue null )
$name string

Cookie 名稱

$defaultValue mixed

當具名 Cookie 不存在時應回傳的值。

return mixed

具名 Cookie 的值。

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

            
has() public method

傳回是否有名稱指定的 Cookie。

請注意,如果 Cookie 被標記為從瀏覽器刪除或其值為空字串,此方法將會回傳 false。

參見 remove()

public boolean has ( $name )
$name string

Cookie 名稱

return boolean

具名 Cookie 是否存在

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

傳回一個值,指示是否已定義方法。

預設實作是呼叫 php 函數 method_exists()。當您實作 php 魔術方法 __call() 時,您可以覆寫此方法。

public boolean hasMethod ( $name )
$name string

方法名稱

return boolean

方法是否已定義

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

            
hasProperty() public method

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

傳回一個值,指示是否已定義屬性。

如果符合以下條件,則屬性為已定義:

  • 類別具有與指定名稱關聯的 getter 或 setter 方法(在這種情況下,屬性名稱不區分大小寫);
  • 類別具有帶有指定名稱的成員變數(當 $checkVars 為 true 時);

參見

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

屬性名稱

$checkVars boolean

是否將成員變數視為屬性

return boolean

屬性是否已定義

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

            
init() public method

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

初始化物件。

在物件以給定的組態初始化後,此方法會在建構函式的結尾被調用。

public void init ( )

                public function init()
{
}

            
offsetExists() public method

傳回是否有名稱指定的 Cookie。

SPL 介面 ArrayAccess 需要此方法。當您使用類似 isset($collection[$name]) 的語法時,它會被隱式地呼叫。

public boolean offsetExists ( $name )
$name string

Cookie 名稱

return boolean

具名 Cookie 是否存在

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

            
offsetGet() public method

傳回具有指定名稱的 Cookie。

SPL 介面 ArrayAccess 需要此方法。當您使用類似 $cookie = $collection[$name]; 的語法時,它會被隱式地呼叫。這等同於 get()

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

Cookie 名稱

return yii\web\Cookie|null

具有指定名稱的 Cookie,如果具名 Cookie 不存在,則為 null。

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

            
offsetSet() public method

將 Cookie 新增至集合。

SPL 介面 ArrayAccess 需要此方法。當您使用類似 $collection[$name] = $cookie; 的語法時,它會被隱式地呼叫。這等同於 add()

public void offsetSet ( $name, $cookie )
$name string

Cookie 名稱

$cookie yii\web\Cookie

要加入的 Cookie

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

            
offsetUnset() public method

移除具名 Cookie。

SPL 介面 ArrayAccess 需要此方法。當您使用類似 unset($collection[$name]) 的語法時,它會被隱式地呼叫。這等同於 remove()

public void offsetUnset ( $name )
$name string

Cookie 名稱

                #[\ReturnTypeWillChange]
public function offsetUnset($name)
{
    $this->remove($name);
}

            
remove() public method

移除一個 Cookie。

如果 $removeFromBrowser 為 true,Cookie 將會從瀏覽器中移除。在這種情況下,將會新增一個過期的 Cookie 到集合中。

public void remove ( $cookie, $removeFromBrowser true )
$cookie yii\web\Cookie|string

要移除的 Cookie 物件或 Cookie 名稱。

$removeFromBrowser boolean

是否從瀏覽器移除 Cookie

throws yii\base\InvalidCallException

如果 Cookie 集合為唯讀

                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

移除所有 Cookie。

public void removeAll ( )
throws yii\base\InvalidCallException

如果 Cookie 集合為唯讀

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

            
toArray() public method

以 PHP 陣列形式傳回集合。

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

集合的陣列表示形式。陣列鍵為 Cookie 名稱,陣列值為對應的 Cookie 物件。

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