類別 yii\web\HeaderCollection
HeaderCollection 被 yii\web\Response 用於維護目前已註冊的 HTTP 標頭。
公開屬性
屬性 | 類型 | 描述 | 定義於 |
---|---|---|---|
$count | integer | 集合中標頭的數量。 | yii\web\HeaderCollection |
$iterator | ArrayIterator | 用於遍歷集合中標頭的迭代器。 | yii\web\HeaderCollection |
公開方法
屬性詳情
方法詳情
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()");
}
定義於: yii\base\BaseObject::__construct()
建構子。
預設實作執行兩件事
- 使用給定的組態
$config
初始化物件。 - 呼叫 init()。
如果子類別中覆寫了此方法,建議
- 建構子的最後一個參數是組態陣列,如這裡的
$config
。 - 在建構子的結尾呼叫父類別實作。
public void __construct ( $config = [] ) | ||
$config | array |
將用於初始化物件屬性的名稱-值對 |
public function __construct($config = [])
{
if (!empty($config)) {
Yii::configure($this, $config);
}
$this->init();
}
定義於: 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);
}
定義於: 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;
}
定義於: 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);
}
}
定義於: 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);
}
}
新增一個標頭。
如果已存在同名的標頭,則新的標頭將附加到其後,而不是取代它。
public $this add ( $name, $value ) | ||
$name | string |
標頭的名稱 |
$value | string |
標頭的值 |
return | $this |
集合物件本身 |
---|
public function add($name, $value)
{
$normalizedName = strtolower($name);
$this->_headers[$normalizedName][] = $value;
if (!\array_key_exists($normalizedName, $this->_originalHeaderNames)) {
$this->_originalHeaderNames[$normalizedName] = $name;
}
return $this;
}
定義於: 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);
}
定義於: 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
。
定義於: yii\base\BaseObject::className()
傳回此類別的完整名稱。
public static string className ( ) | ||
return | string |
此類別的完整名稱。 |
---|
public static function className()
{
return get_called_class();
}
傳回集合中標頭的數量。
此方法為 SPL Countable
介面所要求。 當您使用 count($collection)
時,它會被隱式呼叫。
public integer count ( ) | ||
return | integer |
集合中標頭的數量。 |
---|
#[\ReturnTypeWillChange]
public function count()
{
return $this->getCount();
}
從陣列填入標頭集合。
public void fromArray ( array $array ) | ||
$array | array |
要從中填入的標頭 |
public function fromArray(array $array)
{
foreach ($array as $name => $value) {
$this->set($name, $value);
}
}
傳回具名的標頭。
public string|array|null get ( $name, $default = null, $first = true ) | ||
$name | string |
要傳回的標頭名稱 |
$default | mixed |
如果具名標頭不存在時要傳回的值 |
$first | boolean |
是否僅傳回指定名稱的第一個標頭。 如果為 false,將傳回指定名稱的所有標頭。 |
return | string|array|null |
具名的標頭。 如果 |
---|
public function get($name, $default = null, $first = true)
{
$normalizedName = strtolower($name);
if (isset($this->_headers[$normalizedName])) {
return $first ? reset($this->_headers[$normalizedName]) : $this->_headers[$normalizedName];
}
return $default;
}
傳回集合中標頭的數量。
public integer getCount ( ) | ||
return | integer |
集合中標頭的數量。 |
---|
#[\ReturnTypeWillChange]
public function getCount()
{
return count($this->_headers);
}
傳回用於遍歷集合中標頭的迭代器。
此方法為 SPL 介面 IteratorAggregate 所要求。 當您使用 foreach
遍歷集合時,它會被隱式呼叫。
public ArrayIterator getIterator ( ) | ||
return | ArrayIterator |
用於遍歷集合中標頭的迭代器。 |
---|
#[\ReturnTypeWillChange]
public function getIterator()
{
return new \ArrayIterator($this->_headers);
}
傳回值,指示具名的標頭是否存在。
public boolean has ( $name ) | ||
$name | string |
標頭的名稱 |
return | boolean |
具名標頭是否存在 |
---|
public function has($name)
{
return isset($this->_headers[strtolower($name)]);
}
定義於: 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);
}
定義於: 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()
{
}
傳回是否有名稱指定的標頭。
此方法為 SPL 介面 ArrayAccess 所要求。 當您使用類似 isset($collection[$name])
的語法時,它會被隱式呼叫。
public boolean offsetExists ( $name ) | ||
$name | string |
標頭名稱 |
return | boolean |
具名標頭是否存在 |
---|
#[\ReturnTypeWillChange]
public function offsetExists($name)
{
return $this->has($name);
}
傳回名稱指定的標頭。
此方法為 SPL 介面 ArrayAccess 所要求。 當您使用類似 $header = $collection[$name];
的語法時,它會被隱式呼叫。 這等同於 get()。
public string|null offsetGet ( $name ) | ||
$name | string |
標頭名稱 |
return | string|null |
名稱指定的標頭值;如果具名標頭不存在,則為 null。 |
---|
#[\ReturnTypeWillChange]
public function offsetGet($name)
{
return $this->get($name);
}
將標頭新增至集合。
此方法為 SPL 介面 ArrayAccess 所要求。 當您使用類似 $collection[$name] = $header;
的語法時,它會被隱式呼叫。 這等同於 add()。
public void offsetSet ( $name, $value ) | ||
$name | string |
標頭名稱 |
$value | string |
要新增的標頭值 |
#[\ReturnTypeWillChange]
public function offsetSet($name, $value)
{
$this->set($name, $value);
}
移除具名的標頭。
此方法為 SPL 介面 ArrayAccess 所要求。 當您使用類似 unset($collection[$name])
的語法時,它會被隱式呼叫。 這等同於 remove()。
public void offsetUnset ( $name ) | ||
$name | string |
標頭名稱 |
#[\ReturnTypeWillChange]
public function offsetUnset($name)
{
$this->remove($name);
}
移除標頭。
public array|null remove ( $name ) | ||
$name | string |
要移除的標頭名稱。 |
return | array|null |
已移除標頭的值。 如果標頭不存在,則傳回 Null。 |
---|
public function remove($name)
{
$normalizedName = strtolower($name);
if (isset($this->_headers[$normalizedName])) {
$value = $this->_headers[$normalizedName];
unset($this->_headers[$normalizedName], $this->_originalHeaderNames[$normalizedName]);
return $value;
}
return null;
}
移除所有標頭。
public void removeAll ( ) |
public function removeAll()
{
$this->_headers = [];
$this->_originalHeaderNames = [];
}
新增一個標頭。
如果已存在同名的標頭,則會被取代。
public $this set ( $name, $value = '' ) | ||
$name | string |
標頭的名稱 |
$value | string |
標頭的值 |
return | $this |
集合物件本身 |
---|
public function set($name, $value = '')
{
$normalizedName = strtolower($name);
$this->_headers[$normalizedName] = (array) $value;
$this->_originalHeaderNames[$normalizedName] = $name;
return $this;
}
僅當新標頭尚不存在時才設定。
如果已存在同名的標頭,則會忽略新的標頭。
public $this setDefault ( $name, $value ) | ||
$name | string |
標頭的名稱 |
$value | string |
標頭的值 |
return | $this |
集合物件本身 |
---|
public function setDefault($name, $value)
{
$normalizedName = strtolower($name);
if (empty($this->_headers[$normalizedName])) {
$this->_headers[$normalizedName][] = $value;
$this->_originalHeaderNames[$normalizedName] = $name;
}
return $this;
}
以 PHP 陣列形式傳回集合。
public array toArray ( ) | ||
return | array |
集合的陣列表示法。 陣列鍵是標頭名稱,而陣列值是對應的標頭值。 |
---|
public function toArray()
{
return $this->_headers;
}
以 PHP 陣列形式傳回集合,但不是使用標準化的標頭名稱作為鍵(如 toArray()),而是使用原始標頭名稱(區分大小寫)。
public array toOriginalArray ( ) | ||
return | array |
此集合的陣列表示法。 |
---|
public function toOriginalArray()
{
return \array_map(function ($normalizedName) {
return $this->_headers[$normalizedName];
}, \array_flip($this->_originalHeaderNames));
}
註冊 或 登入 以便發表評論。