0 關注者

類別 yii\web\HeaderCollection

繼承關係yii\web\HeaderCollection » yii\base\BaseObject
實作介面ArrayAccess, Countable, IteratorAggregate, yii\base\Configurable
自版本2.0
原始碼 https://github.com/yiisoft/yii2/blob/master/framework/web/HeaderCollection.php

HeaderCollection 被 yii\web\Response 用於維護目前已註冊的 HTTP 標頭。

公開屬性

隱藏繼承的屬性

屬性 類型 描述 定義於
$count integer 集合中標頭的數量。 yii\web\HeaderCollection
$iterator ArrayIterator 用於遍歷集合中標頭的迭代器。 yii\web\HeaderCollection

公開方法

隱藏繼承的方法

方法 描述 定義於
__call() 調用指定的非類別方法。 yii\base\BaseObject
__construct() 建構子。 yii\base\BaseObject
__get() 返回物件屬性的值。 yii\base\BaseObject
__isset() 檢查屬性是否已設定,即已定義且非 null。 yii\base\BaseObject
__set() 設定物件屬性的值。 yii\base\BaseObject
__unset() 將物件屬性設定為 null。 yii\base\BaseObject
add() 新增一個標頭。 yii\web\HeaderCollection
canGetProperty() 返回一個值,指示屬性是否可以被讀取。 yii\base\BaseObject
canSetProperty() 傳回值,指示是否可以設定屬性。 yii\base\BaseObject
className() 傳回此類別的完整名稱。 yii\base\BaseObject
count() 傳回集合中標頭的數量。 yii\web\HeaderCollection
fromArray() 從陣列填入標頭集合。 yii\web\HeaderCollection
get() 傳回具名的標頭。 yii\web\HeaderCollection
getCount() 傳回集合中標頭的數量。 yii\web\HeaderCollection
getIterator() 傳回用於遍歷集合中標頭的迭代器。 yii\web\HeaderCollection
has() 傳回值,指示具名的標頭是否存在。 yii\web\HeaderCollection
hasMethod() 傳回值,指示是否已定義方法。 yii\base\BaseObject
hasProperty() 傳回值,指示是否已定義屬性。 yii\base\BaseObject
init() 初始化物件。 yii\base\BaseObject
offsetExists() 傳回是否有名稱指定的標頭。 yii\web\HeaderCollection
offsetGet() 傳回名稱指定的標頭。 yii\web\HeaderCollection
offsetSet() 將標頭新增至集合。 yii\web\HeaderCollection
offsetUnset() 移除具名的標頭。 yii\web\HeaderCollection
remove() 移除標頭。 yii\web\HeaderCollection
removeAll() 移除所有標頭。 yii\web\HeaderCollection
set() 新增一個標頭。 yii\web\HeaderCollection
setDefault() 僅當新標頭尚不存在時才設定。 yii\web\HeaderCollection
toArray() 以 PHP 陣列形式傳回集合。 yii\web\HeaderCollection
toOriginalArray() 以 PHP 陣列形式傳回集合,但不是使用標準化的標頭名稱作為鍵(如 toArray()),而是使用原始標頭名稱(區分大小寫)。 yii\web\HeaderCollection

屬性詳情

隱藏繼承的屬性

$count public 唯讀 屬性

集合中標頭的數量。

public integer getCount ( )
$iterator public 唯讀 屬性

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

方法詳情

隱藏繼承的方法

__call() public 方法

定義於: 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 方法

定義於: 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();
}

            
__get() public 方法

定義於: 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 方法

定義於: 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 方法

定義於: 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 方法

定義於: 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 方法

新增一個標頭。

如果已存在同名的標頭,則新的標頭將附加到其後,而不是取代它。

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;
}

            
canGetProperty() public 方法

定義於: 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 方法

定義於: 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 靜態方法
自 2.0.14 版起已過時。 在 PHP >=5.5 上,請改用 ::class

定義於: yii\base\BaseObject::className()

傳回此類別的完整名稱。

public static string className ( )
return string

此類別的完整名稱。

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

            
count() public 方法

傳回集合中標頭的數量。

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

public integer count ( )
return integer

集合中標頭的數量。

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

            
fromArray() public 方法 (自 2.0.3 版起可用)

從陣列填入標頭集合。

public void fromArray ( array $array )
$array array

要從中填入的標頭

                public function fromArray(array $array)
{
    foreach ($array as $name => $value) {
        $this->set($name, $value);
    }
}

            
get() public 方法

傳回具名的標頭。

public string|array|null get ( $name, $default null, $first true )
$name string

要傳回的標頭名稱

$default mixed

如果具名標頭不存在時要傳回的值

$first boolean

是否僅傳回指定名稱的第一個標頭。 如果為 false,將傳回指定名稱的所有標頭。

return string|array|null

具名的標頭。 如果 $first 為 true,將傳回字串;如果 $first 為 false,將傳回陣列。

                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;
}

            
getCount() public 方法

傳回集合中標頭的數量。

public integer getCount ( )
return integer

集合中標頭的數量。

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

            
getIterator() public 方法

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

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

public ArrayIterator getIterator ( )
return ArrayIterator

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

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

            
has() public 方法

傳回值,指示具名的標頭是否存在。

public boolean has ( $name )
$name string

標頭的名稱

return boolean

具名標頭是否存在

                public function has($name)
{
    return isset($this->_headers[strtolower($name)]);
}

            
hasMethod() public 方法

定義於: 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 方法

定義於: 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 方法

定義於: yii\base\BaseObject::init()

初始化物件。

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

public void init ( )

                public function init()
{
}

            
offsetExists() public 方法

傳回是否有名稱指定的標頭。

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

public boolean offsetExists ( $name )
$name string

標頭名稱

return boolean

具名標頭是否存在

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

            
offsetGet() public 方法

傳回名稱指定的標頭。

此方法為 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);
}

            
offsetSet() public 方法

將標頭新增至集合。

此方法為 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);
}

            
offsetUnset() public 方法

移除具名的標頭。

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

public void offsetUnset ( $name )
$name string

標頭名稱

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

            
remove() public 方法

移除標頭。

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;
}

            
removeAll() public 方法

移除所有標頭。

public void removeAll ( )

                public function removeAll()
{
    $this->_headers = [];
    $this->_originalHeaderNames = [];
}

            
set() public 方法

新增一個標頭。

如果已存在同名的標頭,則會被取代。

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;
}

            
setDefault() public 方法

僅當新標頭尚不存在時才設定。

如果已存在同名的標頭,則會忽略新的標頭。

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;
}

            
toArray() public 方法

以 PHP 陣列形式傳回集合。

public array toArray ( )
return array

集合的陣列表示法。 陣列鍵是標頭名稱,而陣列值是對應的標頭值。

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

            
toOriginalArray() public method (自 2.0.45 版本起可用)

以 PHP 陣列形式傳回集合,但不是使用標準化的標頭名稱作為鍵(如 toArray()),而是使用原始標頭名稱(區分大小寫)。

public array toOriginalArray ( )
return array

此集合的陣列表示法。

                public function toOriginalArray()
{
    return \array_map(function ($normalizedName) {
        return $this->_headers[$normalizedName];
    }, \array_flip($this->_originalHeaderNames));
}