Behavior
API reference › yii › base › Behavior
7 subclasses
Behavior is the base class for all behavior classes.
A behavior can be used to enhance the functionality of an existing component without modifying its code. In particular, it can “inject” its own methods and properties into the component and make them directly accessible via the component. It can also respond to the events triggered in the component and thus intercept the normal code execution.
For more details and usage information on Behavior, see the guide article on behaviors.
Properties
Section titled “Properties”| Property | Type | Description | Defined by |
|---|---|---|---|
$owner | yii\ | The owner of this behavior Default: null. | Behavior |
Methods
Section titled “Methods”| Method | Description | Defined by |
|---|---|---|
__construct() | Constructor. | BaseObject |
__call() | Calls the named method which is not a class method. | BaseObject |
__get() | Returns the value of an object property. | BaseObject |
__isset() | Checks if a property is set, i.e. defined and not null. | BaseObject |
__set() | Sets value of an object property. | BaseObject |
__unset() | Sets an object property to null. | BaseObject |
attach() | Attaches the behavior object to the component. | Behavior |
canGetProperty() | Returns a value indicating whether a property can be read. | BaseObject |
canSetProperty() | Returns a value indicating whether a property can be set. | BaseObject |
className() static | Returns the fully qualified name of this class. | BaseObject |
detach() | Detaches the behavior object from the component. | Behavior |
events() | Declares event handlers for the $owner’s events. | Behavior |
hasMethod() | Returns a value indicating whether a method is defined. | BaseObject |
hasProperty() | Returns a value indicating whether a property is defined. | BaseObject |
init() | Initializes the object. | BaseObject |
Method details
Section titled “Method details”attach()
Section titled “attach() ”public function attach(yii\base\T $owner): mixedAttaches the behavior object to the component.
The default implementation will set the $owner property
and attach event handlers as declared in events().
Make sure you call the parent implementation if you override this method.
Parameters
| Parameter | Type | Description |
|---|---|---|
$owner | yii\ | The component that this behavior is to be attached to. |
Returns
mixed
detach()
Section titled “detach() ”public function detach(): mixedDetaches the behavior object from the component.
The default implementation will unset the $owner property
and detach event handlers declared in events().
Make sure you call the parent implementation if you override this method.
Returns
mixed
events()
Section titled “events() ”public function events(): arrayDeclares event handlers for the $owner’s events.
Child classes may override this method to declare what PHP callbacks should
be attached to the events of the $owner component.
The callbacks will be attached to the $owner’s events when the behavior is
attached to the owner; and they will be detached from the events when
the behavior is detached from the component.
The callbacks can be any of the following:
- method in this behavior:
'handleClick', equivalent to[$this, 'handleClick'] - object method:
[$object, 'handleClick'] - static method:
['Page', 'handleClick'] - anonymous function:
function ($event) { ... }
The following is an example:
[ Model::EVENT_BEFORE_VALIDATE => 'myBeforeValidate', Model::EVENT_AFTER_VALIDATE => 'myAfterValidate',]Returns
array: Events (array keys) and the corresponding event handler methods (array values).