Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Use of Component Behavior</strong> </p> <p>A component supports the <a href="http://en.wikipedia.org/wiki/Mixin" rel="nofollow">mixin</a> pattern and can be attached with one or several behaviors. A behavior is an object whose methods can be 'inherited' by its attached component through the means of collecting functionality instead of specialization (i.e., normal class inheritance). A component can be attached with several behaviors and thus achieve 'multiple inheritance'.</p> <p>Behavior classes must implement the <code>IBehavior</code> interface. Most behaviors can extend from the <code>CBehavior</code> base class. If a behavior needs to be attached to a model, it may also extend from <a href="http://www.yiiframework.com/doc/api/1.1/CModelBehavior" rel="nofollow">CModelBehavior</a> or <a href="http://www.yiiframework.com/doc/api/1.1/CActiveRecordBehavior" rel="nofollow">CActiveRecordBehavior</a> which implements additional features specifc for models.</p> <p>To use a behavior, it must be attached to a component first by calling the behavior's <code>attach()</code> method. Then we can call a behavior method via the component:</p> <pre><code>// $name uniquely identifies the behavior in the component $component-&gt;attachBehavior($name,$behavior); // test() is a method of $behavior $component-&gt;test(); </code></pre> <p>An attached behavior can be accessed like a normal property of the component. For example, if a behavior named tree is attached to a component, we can obtain the reference to this behavior object using:</p> <pre><code>$behavior=$component-&gt;tree; // equivalent to the following: // $behavior=$component-&gt;asa('tree'); </code></pre> <p>A behavior can be temporarily disabled so that its methods are not available via the component. For example,</p> <pre><code>$component-&gt;disableBehavior($name); // the following statement will throw an exception $component-&gt;test(); $component-&gt;enableBehavior($name); // it works now $component-&gt;test(); </code></pre> <p>It is possible that two behaviors attached to the same component have methods of the same name. In this case, the method of the first attached behavior will take precedence.</p> <p>When used together with <a href="http://www.yiiframework.com/doc/guide/1.1/en/basics.component#component-event" rel="nofollow">events</a>, behaviors are even more powerful. A behavior, when being attached to a component, can attach some of its methods to some events of the component. By doing so, the behavior gets a chance to observe or change the normal execution flow of the component.</p> <p>A behavior's properties can also be accessed via the component it is attached to. The properties include both the public member variables and the properties defined via getters and/or setters of the behavior. For example, if a behavior has a property named xyz and the behavior is attached to a component $a. Then we can use the expression <code>$a-&gt;xyz</code> to access the behavior's property.</p> <p>More reading:<br> <a href="http://www.yiiframework.com/wiki/44/behaviors-events" rel="nofollow">http://www.yiiframework.com/wiki/44/behaviors-events</a><br> <a href="http://www.ramirezcobos.com/2010/11/19/how-to-create-a-yii-behavior/" rel="nofollow">http://www.ramirezcobos.com/2010/11/19/how-to-create-a-yii-behavior/</a></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload