문제

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!

도움이 되었습니까?

해결책

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

다른 팁

In Yii, an application works through interaction of different objects, These object can be simply considered "components" or "building blocks" of the application. A component is simply an object that has been writing to perform or facilitate a particular task in a Yii application. If you have look at "Typical workflow of a Yii application" on [http://www.yiiframework.com/doc/guide/1.1/en/basics.mvc][1]

you ll realize that apart from view and layouts (which are considered scripts) and index.php ,everything that interacts with others is a component. They all perform different tasks when the application is run.

Almost everything in Yii is a component (or derived from CComponent class).

More specifically,

CComponent implements the protocol of defining, using properties and events."

Events : Events allow you to perform a sequence (more than one) of actions when something specific happens within a component. You define an event and attach a number of functions (actions) to that event. Now, whenever that event is raised within the component, all the functions attached to that event are executed. As per my understanding, these are somewhat similar to the concept of hooks in Wordpress.

The specific application of events in a component is defined by Yii as

It is useful when you want to interrupt the normal application flow without extending base classes.

Behaviors : Behaviors are simply Yii's way of providing you with multiple inheritance , which is not supported by PHP5 while doing away with multiple function-same name problem of multiple inheritance. If you want to inherit properties and methods from Class A and B. You extend class A and then attach class B as its behavior, and then you can use all the methods of class B as well. Now, if both A and B contained a function named 'usefulfunction()', all calls to this function will result in execution of 'usefulfunction' from class A only. If both class A and B were added as behaviors to a class, then the 'usefulfunction' call would result in execution of method from the behavior which was attached first.

P.S. (I am not a expert. So please correct me if i am wrong anywhere.)

Perhaps this extra page on their wiki could provide more insight: http://www.yiiframework.com/wiki/44/behaviors-events/

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top