Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

yii components: events and behaviors?

Tags:

php

yii

i'm currently learning the yii framework and have read their documentation.

but i still don't understand the components. what are these. they talk about component events and behaviors.

could someone explain these terms for me and give me real app examples of what a component, its events and behaviors could be?

would be helpful!

like image 856
ajsie Avatar asked Feb 03 '10 02:02

ajsie


1 Answers

A CComponent by itself doesn't do much. It's very much like a QObject in Qt. A CComponent can raise events, and can have delegates to events (through attachEventHandler()).

Regarding behaviours, the manual says:

The methods of the behavior can be invoked as if they belong to the component. Multiple behaviors can be attached to the same component.

To attach a behavior to a component, call attachBehavior; and to detach the behavior from the component, call detachBehavior.

A behavior can be temporarily enabled or disabled by calling enableBehavior or disableBehavior, respectively. When disabled, the behavior methods cannot be invoked via the component.

Starting from version 1.1.0, a behavior's properties (either its public member variables or its properties defined via getters and/or setters) can be accessed through the component it is attached to.

Which gives you the possibility to simulate a signals and slots mechanism, or the strategy pattern (by enabling or disabling behaviours).

Most of the classes in Yii have CComponent as a base class.

As a user, you'll see the benefits they provide through the mechanisms mentioned above when you'll create your own components (under protected/components/).

You can find a good starting point for implementing components here: http://www.yiiframework.com/doc/guide/basics.component

like image 102
Flavius Avatar answered Sep 25 '22 12:09

Flavius