behaviors() публичный Метод

Child classes may override this method to specify the behaviors they want to behave as. The return value of this method should be an array of behavior objects or configurations indexed by behavior names. A behavior configuration can be either a string specifying the behavior class or an array of the following structure: php 'behaviorName' => [ 'class' => 'BehaviorClass', 'property1' => 'value1', 'property2' => 'value2', ] Note that a behavior class must extend from Behavior. Behaviors can be attached using a name or anonymously. When a name is used as the array key, using this name, the behavior can later be retrieved using Component::getBehavior or be detached using Component::detachBehavior. Anonymous behaviors can not be retrieved or detached. Behaviors declared in this method will be attached to the component automatically (on demand).
public behaviors ( ) : array
Результат array the behavior configurations.
 /**
  * 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
0
 public function behaviors()
 {
     return Utils::behaviorMerge(self::className(), parent::behaviors());
 }