0 關注者

類別 yii\i18n\MissingTranslationEvent

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

MissingTranslationEvent 代表 yii\i18n\MessageSource::EVENT_MISSING_TRANSLATION 事件的參數。

公開屬性

隱藏繼承的屬性

屬性 類型 描述 定義於
$category string 訊息所屬的類別 yii\i18n\MissingTranslationEvent
$data mixed 當附加事件處理程序時,傳遞給 yii\base\Component::on() 的資料。 yii\base\Event
$handled boolean 事件是否已被處理。 yii\base\Event
$language string 訊息要翻譯成的語言 ID (例如:en-US) yii\i18n\MissingTranslationEvent
$message string 要翻譯的訊息。 yii\i18n\MissingTranslationEvent
$name string 事件名稱。 yii\base\Event
$sender object|null 此事件的發送者。 yii\base\Event
$translatedMessage string|null 已翻譯的訊息。 yii\i18n\MissingTranslationEvent

公開方法

隱藏繼承的方法

方法 描述 定義於
__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
hasHandlers() 傳回值,指出是否有任何處理程序附加到指定的類別層級事件。 yii\base\Event
hasMethod() 傳回值,指出是否已定義方法。 yii\base\BaseObject
hasProperty() 傳回值,指出是否已定義屬性。 yii\base\BaseObject
init() 初始化物件。 yii\base\BaseObject
off() 從類別層級事件分離事件處理程序。 yii\base\Event
offAll() 分離所有已註冊的類別層級事件處理程序。 yii\base\Event
on() 將事件處理程序附加到類別層級事件。 yii\base\Event
trigger() 觸發類別層級事件。 yii\base\Event

屬性詳細資訊

隱藏繼承的屬性

$category public property

訊息所屬的類別

public string $category null
$language public property

訊息要翻譯成的語言 ID (例如:en-US)

public string $language null
$message public property

要翻譯的訊息。事件處理器可能會使用此訊息來提供備用翻譯,並在可能的情況下設定 $translatedMessage

public string $message null
$translatedMessage public property

已翻譯的訊息。事件處理器可能會使用 $message 的翻譯版本覆寫此屬性(如果可能)。如果未設定(null),則表示訊息未翻譯。

方法詳情

隱藏繼承的方法

__call() public method

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

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

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

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

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

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

            
canGetProperty() public method

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

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

            
hasHandlers() public static method

定義於: yii\base\Event::hasHandlers()

傳回值,指出是否有任何處理程序附加到指定的類別層級事件。

請注意,此方法也會檢查所有父類別,以查看是否有任何處理器附加到具名事件。

public static boolean hasHandlers ( $class, $name )
$class string|object

物件或完整類別名稱,指定類別層級事件。

$name string

事件名稱。

return boolean

是否有任何處理器附加到事件。

                public static function hasHandlers($class, $name)
{
    if (empty(self::$_eventWildcards) && empty(self::$_events[$name])) {
        return false;
    }
    if (is_object($class)) {
        $class = get_class($class);
    } else {
        $class = ltrim($class, '\\');
    }
    $classes = array_merge(
        [$class],
        class_parents($class, true),
        class_implements($class, true)
    );
    // regular events
    foreach ($classes as $className) {
        if (!empty(self::$_events[$name][$className])) {
            return true;
        }
    }
    // wildcard events
    foreach (self::$_eventWildcards as $nameWildcard => $classHandlers) {
        if (!StringHelper::matchWildcard($nameWildcard, $name, ['escape' => false])) {
            continue;
        }
        foreach ($classHandlers as $classWildcard => $handlers) {
            if (empty($handlers)) {
                continue;
            }
            foreach ($classes as $className) {
                if (StringHelper::matchWildcard($classWildcard, $className, ['escape' => false])) {
                    return true;
                }
            }
        }
    }
    return false;
}

            
hasMethod() public method

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

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

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

初始化物件。

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

public void init ( )

                public function init()
{
}

            
off() public static method

定義於: yii\base\Event::off()

從類別層級事件分離事件處理程序。

此方法與 on() 相反。

注意:如果為類別名稱或事件名稱傳遞了萬用字元模式,則只會移除使用此萬用字元註冊的處理器,而使用與此萬用字元匹配的純名稱註冊的處理器將會保留。

另請參閱 on()

public static boolean off ( $class, $name, $handler null )
$class string

需要從中分離事件處理器的完整類別名稱。

$name string

事件名稱。

$handler callable|null

要移除的事件處理器。如果為 null,則會移除附加到具名事件的所有處理器。

return boolean

是否找到並分離了處理器。

                public static function off($class, $name, $handler = null)
{
    $class = ltrim($class, '\\');
    if (empty(self::$_events[$name][$class]) && empty(self::$_eventWildcards[$name][$class])) {
        return false;
    }
    if ($handler === null) {
        unset(self::$_events[$name][$class]);
        unset(self::$_eventWildcards[$name][$class]);
        return true;
    }
    // plain event names
    if (isset(self::$_events[$name][$class])) {
        $removed = false;
        foreach (self::$_events[$name][$class] as $i => $event) {
            if ($event[0] === $handler) {
                unset(self::$_events[$name][$class][$i]);
                $removed = true;
            }
        }
        if ($removed) {
            self::$_events[$name][$class] = array_values(self::$_events[$name][$class]);
            return true;
        }
    }
    // wildcard event names
    $removed = false;
    if (isset(self::$_eventWildcards[$name][$class])) {
        foreach (self::$_eventWildcards[$name][$class] as $i => $event) {
            if ($event[0] === $handler) {
                unset(self::$_eventWildcards[$name][$class][$i]);
                $removed = true;
            }
        }
        if ($removed) {
            self::$_eventWildcards[$name][$class] = array_values(self::$_eventWildcards[$name][$class]);
            // remove empty wildcards to save future redundant regex checks :
            if (empty(self::$_eventWildcards[$name][$class])) {
                unset(self::$_eventWildcards[$name][$class]);
                if (empty(self::$_eventWildcards[$name])) {
                    unset(self::$_eventWildcards[$name]);
                }
            }
        }
    }
    return $removed;
}

            
offAll() public static method (自 2.0.10 版本起可用)

定義於: yii\base\Event::offAll()

分離所有已註冊的類別層級事件處理程序。

另請參閱

public static void offAll ( )

                public static function offAll()
{
    self::$_events = [];
    self::$_eventWildcards = [];
}

            
on() public static method

定義於: yii\base\Event::on()

將事件處理程序附加到類別層級事件。

當觸發類別層級事件時,將會調用附加到該類別和所有父類別的事件處理器。

例如,以下程式碼將事件處理器附加到 ActiveRecordafterInsert 事件

Event::on(ActiveRecord::class, ActiveRecord::EVENT_AFTER_INSERT, function ($event) {
    Yii::trace(get_class($event->sender) . ' is inserted.');
});

對於每次成功的 ActiveRecord 插入,都將調用處理器。

自 2.0.14 版本起,您可以將類別名稱或事件名稱指定為萬用字元模式

Event::on('app\models\db\*', '*Insert', function ($event) {
    Yii::trace(get_class($event->sender) . ' is inserted.');
});

有關如何宣告事件處理器的更多詳細資訊,請參閱 yii\base\Component::on()

另請參閱 off()

public static void on ( $class, $name, $handler, $data null, $append true )
$class string

事件處理器需要附加到的完整類別名稱。

$name string

事件名稱。

$handler callable

事件處理器。

$data mixed

當觸發事件時,要傳遞給事件處理器的資料。當調用事件處理器時,可以透過 yii\base\Event::$data 存取此資料。

$append boolean

是否將新的事件處理器附加到現有處理器列表的末尾。如果為 false,則新的處理器將插入到現有處理器列表的開頭。

                public static function on($class, $name, $handler, $data = null, $append = true)
{
    $class = ltrim($class, '\\');
    if (strpos($class, '*') !== false || strpos($name, '*') !== false) {
        if ($append || empty(self::$_eventWildcards[$name][$class])) {
            self::$_eventWildcards[$name][$class][] = [$handler, $data];
        } else {
            array_unshift(self::$_eventWildcards[$name][$class], [$handler, $data]);
        }
        return;
    }
    if ($append || empty(self::$_events[$name][$class])) {
        self::$_events[$name][$class][] = [$handler, $data];
    } else {
        array_unshift(self::$_events[$name][$class], [$handler, $data]);
    }
}

            
trigger() public static method

定義於: yii\base\Event::trigger()

觸發類別層級事件。

此方法將導致調用附加到指定類別及其所有父類別的具名事件的事件處理器。

public static void trigger ( $class, $name, $event null )
$class string|object

物件或完整類別名稱,指定類別層級事件。

$name string

事件名稱。

$event yii\base\Event|null

事件參數。如果未設定,將會建立預設的 yii\base\Event 物件。

                public static function trigger($class, $name, $event = null)
{
    $wildcardEventHandlers = [];
    foreach (self::$_eventWildcards as $nameWildcard => $classHandlers) {
        if (!StringHelper::matchWildcard($nameWildcard, $name)) {
            continue;
        }
        $wildcardEventHandlers = array_merge($wildcardEventHandlers, $classHandlers);
    }
    if (empty(self::$_events[$name]) && empty($wildcardEventHandlers)) {
        return;
    }
    if ($event === null) {
        $event = new static();
    }
    $event->handled = false;
    $event->name = $name;
    if (is_object($class)) {
        if ($event->sender === null) {
            $event->sender = $class;
        }
        $class = get_class($class);
    } else {
        $class = ltrim($class, '\\');
    }
    $classes = array_merge(
        [$class],
        class_parents($class, true),
        class_implements($class, true)
    );
    foreach ($classes as $class) {
        $eventHandlers = [];
        foreach ($wildcardEventHandlers as $classWildcard => $handlers) {
            if (StringHelper::matchWildcard($classWildcard, $class, ['escape' => false])) {
                $eventHandlers = array_merge($eventHandlers, $handlers);
                unset($wildcardEventHandlers[$classWildcard]);
            }
        }
        if (!empty(self::$_events[$name][$class])) {
            $eventHandlers = array_merge($eventHandlers, self::$_events[$name][$class]);
        }
        foreach ($eventHandlers as $handler) {
            $event->data = $handler[1];
            call_user_func($handler[0], $event);
            if ($event->handled) {
                return;
            }
        }
    }
}