類別 yii\caching\ExpressionDependency
ExpressionDependency 代表基於 PHP 表達式結果的依賴關係。
ExpressionDependency 將使用 eval()
來評估 PHP 表達式。如果且僅當表達式的結果與儲存資料到快取時評估的結果相同,則依賴關係會被報告為未變更。
PHP 表達式可以是任何具有值的 PHP 程式碼。要了解更多關於表達式的資訊,請參閱 php 手冊。
有關快取的更多詳細資訊和使用方法,請參閱 快取概述指南文章。
公共屬性
屬性 | 類型 | 描述 | 定義於 |
---|---|---|---|
$data | mixed | 依賴關係資料,儲存在快取中,稍後與最新的依賴關係資料進行比較。 | yii\caching\Dependency |
$expression | string | PHP 表達式的字串表示形式,其結果用於確定依賴關係。 | yii\caching\ExpressionDependency |
$params | mixed | 與此依賴關係關聯的自訂參數。 | yii\caching\ExpressionDependency |
$reusable | boolean | 此依賴關係是否可重複使用。 | yii\caching\Dependency |
公共方法
方法 | 描述 | 定義於 |
---|---|---|
__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 |
evaluateDependency() | 透過產生和儲存與依賴關係相關的資料來評估依賴關係。 | yii\caching\Dependency |
getHasChanged() | 傳回一個值,指示依賴關係是否已變更。 | yii\caching\Dependency |
hasMethod() | 傳回一個值,指示是否已定義方法。 | yii\base\BaseObject |
hasProperty() | 傳回一個值,指示是否已定義屬性。 | yii\base\BaseObject |
init() | 初始化物件。 | yii\base\BaseObject |
isChanged() | 檢查依賴關係是否已變更。 | yii\caching\Dependency |
resetReusableData() | 重設可重複使用依賴關係的所有快取資料。 | yii\caching\Dependency |
保護方法
方法 | 描述 | 定義於 |
---|---|---|
generateDependencyData() | 產生確定依賴關係是否已變更所需的資料。 | yii\caching\ExpressionDependency |
generateReusableHash() | 產生可用於檢索可重複使用依賴關係資料的唯一雜湊值。 | yii\caching\Dependency |
屬性詳細資訊
PHP 表達式的字串表示形式,其結果用於確定依賴關係。 PHP 表達式可以是任何評估為值的 PHP 程式碼。要了解更多關於表達式的資訊,請參閱 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()");
}
定義於: 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);
}
}
定義於: 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();
}
public void evaluateDependency ( $cache ) | ||
$cache | yii\caching\CacheInterface |
目前正在評估此相依性的快取元件 |
public function evaluateDependency($cache)
{
if ($this->reusable) {
$hash = $this->generateReusableHash();
if (!array_key_exists($hash, self::$_reusableData)) {
self::$_reusableData[$hash] = $this->generateDependencyData($cache);
}
$this->data = self::$_reusableData[$hash];
} else {
$this->data = $this->generateDependencyData($cache);
}
}
產生確定依賴關係是否已變更所需的資料。
此方法回傳 PHP 表達式的結果。
protected mixed generateDependencyData ( $cache ) | ||
$cache | yii\caching\CacheInterface |
目前正在評估此相依性的快取元件 |
return | mixed |
判斷相依性是否已變更所需的資料。 |
---|
protected function generateDependencyData($cache)
{
return eval("return {$this->expression};");
}
protected string generateReusableHash ( ) | ||
return | string |
此快取相依性的唯一雜湊值。 |
---|
protected function generateReusableHash()
{
$clone = clone $this;
$clone->data = null; // https://github.com/yiisoft/yii2/issues/3052
try {
$serialized = serialize($clone);
} catch (\Exception $e) {
// unserializable properties are nulled
foreach ($clone as $name => $value) {
if (is_object($value) && $value instanceof \Closure) {
$clone->{$name} = null;
}
}
$serialized = serialize($clone);
}
return sha1($serialized);
}
定義於: yii\caching\Dependency::getHasChanged()
傳回一個值,指示依賴關係是否已變更。
public boolean getHasChanged ( $cache ) | ||
$cache | yii\caching\CacheInterface |
目前正在評估此相依性的快取元件 |
return | boolean |
相依性是否已變更。 |
---|
public function getHasChanged($cache)
{
return $this->isChanged($cache);
}
定義於: 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()
{
}
定義於: yii\caching\Dependency::isChanged()
檢查依賴關係是否已變更。
public boolean isChanged ( $cache ) | ||
$cache | yii\caching\CacheInterface |
目前正在評估此相依性的快取元件 |
return | boolean |
相依性是否已變更。 |
---|
public function isChanged($cache)
{
if ($this->reusable) {
$hash = $this->generateReusableHash();
if (!array_key_exists($hash, self::$_reusableData)) {
self::$_reusableData[$hash] = $this->generateDependencyData($cache);
}
$data = self::$_reusableData[$hash];
} else {
$data = $this->generateDependencyData($cache);
}
return $data !== $this->data;
}
定義於: yii\caching\Dependency::resetReusableData()
重設可重複使用依賴關係的所有快取資料。
public static void resetReusableData ( ) |
public static function resetReusableData()
{
self::$_reusableData = [];
}
註冊 或 登入 以發表評論。