English 中文(简体)
yii components: events and behaviors?
原标题:
  • 时间:2010-02-03 02:44:16
  •  标签:
  • 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!

最佳回答

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/





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签