本文整理匯總了PHP中yii\base\Component::behaviors方法的典型用法代碼示例。如果您正苦於以下問題:PHP Component::behaviors方法的具體用法?PHP Component::behaviors怎麽用?PHP Component::behaviors使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類yii\base\Component
的用法示例。
在下文中一共展示了Component::behaviors方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。
示例1: hasBehavior
/**
* Checks whether or not a subject has a behavior of a certain type attached.
*
* @param \yii\base\Component $subject the subject to check
* @param string|\yii\base\Behavior $behavior either the class name or an instance of the behavior
* @param bool $throwException if set to true, an exception will be thrown if the
* subject doesn't have the behavior attached
* @return bool true if attached
* @throws \yii\base\InvalidParamException when subject is of wrong type
* @throws \yii\base\InvalidConfigException if desired and behavior missing
*/
public static function hasBehavior($subject, $behavior, $throwException = false)
{
//only components allowed
if (!$subject instanceof \yii\base\Component) {
throw new InvalidParamException(Yii::t('app', 'Subject must extend Component'));
}
//prepare vars
$behavior = $behavior instanceof \yii\base\Behavior ? $behavior::className() : $behavior;
//check if behavior is attached
$found = false;
foreach ($subject->behaviors() as $name => $config) {
$className = is_array($config) ? $config['class'] : $config;
if (strcmp($className, $behavior) === 0) {
$found = true;
break;
}
}
if ($throwException && !$found) {
$msg = Yii::t('app', '{subject} needs to have behavior {behavior} attached', ['subject' => $subject->className(), 'behavior' => $behavior]);
throw new InvalidConfigException($msg);
}
return $found;
}
示例2: behaviors
public function behaviors()
{
return Utils::behaviorMerge(self::className(), parent::behaviors());
}