類別 yii\web\CookieCollection
CookieCollection 維護目前請求中可用的 Cookie。
關於 CookieCollection 的更多詳細資訊和使用方法,請參閱處理 Cookie 的指南文章。
公開屬性
屬性 | 類型 | 描述 | 定義於 |
---|---|---|---|
$count | integer | 集合中 Cookie 的數量。 | yii\web\CookieCollection |
$iterator | \ArrayIterator |
用於遍歷集合中 Cookie 的迭代器。 | yii\web\CookieCollection |
$readOnly | boolean | 此集合是否為唯讀。 | yii\web\CookieCollection |
公開方法
屬性詳細資訊
方法詳細資訊
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()");
}
建構子。
public void __construct ( $cookies = [], $config = [] ) | ||
$cookies | array |
此集合初始包含的 Cookie。這應該是一個名稱-值對的陣列。 |
$config | array |
將用於初始化物件屬性的名稱-值對 |
public function __construct($cookies = [], $config = [])
{
$this->_cookies = $cookies;
parent::__construct($config);
}
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);
}
Defined in: yii\base\BaseObject::__isset()
檢查屬性是否已設定,即已定義且非 null。
請勿直接呼叫此方法,因為它是 PHP 的魔術方法,當執行 isset($object->property)
時會被隱式地呼叫。
請注意,如果屬性未定義,將會回傳 false。
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;
}
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);
}
}
Defined in: yii\base\BaseObject::__unset()
將物件屬性設定為 null。
請勿直接呼叫此方法,因為它是 PHP 的魔術方法,當執行 unset($object->property)
時會被隱式地呼叫。
請注意,如果屬性未定義,此方法將不會執行任何操作。如果屬性為唯讀,則會拋出例外。
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);
}
}
向集合中新增一個 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;
}
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);
}
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);
}
::class
。
Defined in: yii\base\BaseObject::className()
傳回此類別的完整限定名稱。
public static string className ( ) | ||
return | string |
此類別的完整限定名稱。 |
---|
public static function className()
{
return get_called_class();
}
傳回集合中 Cookie 的數量。
SPL Countable
介面需要此方法。當您使用 count($collection)
時,它會被隱式地呼叫。
public integer count ( ) | ||
return | integer |
集合中 Cookie 的數量。 |
---|
#[\ReturnTypeWillChange]
public function count()
{
return $this->getCount();
}
從陣列填充 Cookie 集合。
public void fromArray ( array $array ) | ||
$array | array |
要從中填充的 Cookie |
public function fromArray(array $array)
{
$this->_cookies = $array;
}
傳回具有指定名稱的 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;
}
傳回集合中 Cookie 的數量。
public integer getCount ( ) | ||
return | integer |
集合中 Cookie 的數量。 |
---|
public function getCount()
{
return count($this->_cookies);
}
傳回用於遍歷集合中 Cookie 的迭代器。
SPL 介面 IteratorAggregate 需要此方法。當您使用 foreach
遍歷集合時,它會被隱式地呼叫。
public \ArrayIterator | ||
return | \ArrayIterator |
用於遍歷集合中 Cookie 的迭代器。 |
---|
#[\ReturnTypeWillChange]
public function getIterator()
{
return new ArrayIterator($this->_cookies);
}
傳回具名 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;
}
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()
)
);
}
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);
}
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);
}
public void init ( ) |
public function init()
{
}
傳回是否有名稱指定的 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);
}
傳回具有指定名稱的 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);
}
將 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);
}
移除具名 Cookie。
SPL 介面 ArrayAccess 需要此方法。當您使用類似 unset($collection[$name])
的語法時,它會被隱式地呼叫。這等同於 remove()。
public void offsetUnset ( $name ) | ||
$name | string |
Cookie 名稱 |
#[\ReturnTypeWillChange]
public function offsetUnset($name)
{
$this->remove($name);
}
移除一個 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]);
}
}
移除所有 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 = [];
}
以 PHP 陣列形式傳回集合。
public yii\web\Cookie[] toArray ( ) | ||
return | yii\web\Cookie[] |
集合的陣列表示形式。陣列鍵為 Cookie 名稱,陣列值為對應的 Cookie 物件。 |
---|
public function toArray()
{
return $this->_cookies;
}
註冊 或 登入 以發表評論。