0 關注者

類別 yii\web\Link

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

Link 代表在 JSON Hypermedia API Language 中定義的連結物件。

公共屬性

隱藏繼承的屬性

屬性 類型 描述 定義於
$href string 一個 URI RFC3986 或 URI 模板 RFC6570 yii\web\Link
$hreflang string 目標資源的語言 yii\web\Link
$name string 用於選擇共享相同關聯類型的連結物件的輔助鍵 yii\web\Link
$profile string 一個 URI,提示目標資源的配置。 yii\web\Link
$templated boolean 一個值,指示 $href 是指 URI 還是 URI 模板。 yii\web\Link
$title string 描述連結的標籤 yii\web\Link
$type string 指示在取消引用目標資源時預期的媒體類型的提示 yii\web\Link

公共方法

隱藏繼承的方法

方法 描述 定義於
__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
canGetProperty() 傳回一個值,指示是否可以讀取屬性。 yii\base\BaseObject
canSetProperty() 傳回一個值,指示是否可以設定屬性。 yii\base\BaseObject
className() 傳回此類別的完整限定名稱。 yii\base\BaseObject
hasMethod() 傳回一個值,指示是否定義了方法。 yii\base\BaseObject
hasProperty() 傳回一個值,指示是否定義了屬性。 yii\base\BaseObject
init() 初始化物件。 yii\base\BaseObject
serialize() 將連結列表序列化為正確的陣列格式。 yii\web\Link

常數

隱藏繼承的常數

常數 描述 定義於
REL_SELF 'self' 自我連結。 yii\web\Link

屬性詳細資訊

隱藏繼承的屬性

$href 公共屬性

一個 URI RFC3986 或 URI 模板 RFC6570。此屬性為必填。

public string $href null
$hreflang 公共屬性

目標資源的語言

public string $hreflang null
$name 公共屬性

用於選擇共享相同關聯類型的連結物件的輔助鍵

public string $name null
$profile public 屬性

一個 URI,提示目標資源的配置。

public string $profile null
$templated public 屬性

一個值,指示 $href 是指 URI 還是 URI 模板。

public boolean $templated false
$title public 屬性

描述連結的標籤

public string $title null
$type public 屬性

指示在取消引用目標資源時預期的媒體類型的提示

public string $type null

方法詳情

隱藏繼承的方法

__call() public 方法

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

調用不是類別方法的命名方法。

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

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

方法名稱

$params 陣列

方法參數

回傳 mixed

方法回傳值

拋出 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 陣列

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

                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

屬性名稱

回傳 mixed

屬性值

拋出 yii\base\UnknownPropertyException

如果屬性未定義

拋出 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

屬性名稱或事件名稱

回傳 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

屬性值

拋出 yii\base\UnknownPropertyException

如果屬性未定義

拋出 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

屬性名稱

拋出 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);
    }
}

            
canGetProperty() public 方法

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

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

如果屬性是可讀取的

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

另請參閱 canSetProperty()

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

屬性名稱

$checkVars boolean

是否將成員變數視為屬性

回傳 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

是否將成員變數視為屬性

回傳 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 ( )
回傳 string

此類別的完整限定名稱。

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

            
hasMethod() public 方法

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

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

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

public boolean hasMethod ( $name )
$name string

方法名稱

回傳 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

是否將成員變數視為屬性

回傳 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()
{
}

            
serialize() public 靜態方法

將連結列表序列化為正確的陣列格式。

public static array serialize ( array $links )
$links 陣列

要序列化的連結

回傳 陣列

連結的正確陣列表示形式。

                public static function serialize(array $links)
{
    foreach ($links as $rel => $link) {
        if (is_array($link)) {
            $links[$rel] = self::serialize($link);
        } elseif ($link instanceof self) {
            $links[$rel] = array_filter((array)$link);
        } else {
            $links[$rel] = ['href' => $link];
        }
    }
    return $links;
}