PHP 的 Prado 框架学习笔记(一):自定义控件的属性和事件

最近在学习 PHP 的 Prado 框架,涉及到了自定义控件属性和事件的知识,因为最初对 Prado 不甚了解,同时网上能够找到的相关学习资料少的可怜,可怜的我彻底的郁闷了好几天,最后通过啃框架源码,在“TComponent”类的注释中发现了属性和事件的定义规则和执行原理。为了方便大家学习现将英文资料翻译如下,同时,因本人翻译能力有限,翻译结果可能有误,请参照原文进行学习(原文附后):

转载请保留文章出处:http://www.cnblogs.com/thinksea/articles/2023775.html

 

TComponent 类

TComponent 是所有 PRADO 组件的基类。TComponent 定义了使用属性和事件的协议。

属性定义了一个getter方法​​,和/或setter方法​。可以像访问普通对象成员一样访问属性。读取或写入属性将导致调用相应的getter或setter方法,例如:


1 $a=$this->Text; // 等价于 $a=$this->getText();
2 $this->Text='abc'; // 等价于 $this->setText('abc');
getter和setter方法​​的签名如下,

1 // getter方法​​,定义了一个可读的属性'Text'
2 function getText() { ... }
3 // setter方法​​,定义了一个可写的属性 'Text',通过参数 $value 给属性赋值。
4 function setText($value) { ... }
属性名不区分大小写。建议书写成单词串联格式,其中每个单词首字母要大写(例如:DisplayMode, ItemStyle)。

事件被定义成方法的样子,其命名以“on”为前缀。事件名称就是方法名,并且不区分大小写。一个事件允许挂接一个或多个方法(称为事件处理程序)。通过调用mailto:%7B@link raiseEvent}方法提交事件,按照挂接到事件的顺序自动调用事件处理程序。事件处理程序必须具有如下签名形式,


1 function eventHandlerFuncName($sender,$param) { ... }
其中 $sender 引用(指向)了提交事件的对象,$param 引用(指向)一个结构,其中可能包含特定事件信息。要引发一个组件的事件(这里假设名字是“Click”),使用

1 $component->raiseEvent('OnClick');
要挂接一个事件处理程序到事件,请使用下列方法之一,

1 $component->OnClick=$callback; // 或 $component->OnClick->add($callback);
2 $$component->attachEventHandler('OnClick',$callback);
  前两个方式利用 $component->OnClick 为“OnClick”事件引用事件处理程序列表。变量 $callback 包含事件处理程序的定义,可以是一个指向全局方法名称的字符串,或者一个数组,如果是数组,第一个元素引用一个对象,第二个元素是一个可以通过对象(第一个元素)直接访问的方法名称/路径,例如:
  • 'buttonClicked' : buttonClicked($sender,$param);
  • array($object,'buttonClicked') : $object->buttonClicked($sender,$param);
  • array($object,'MainContent.SubmitButton.buttonClicked') : $object->MainContent->SubmitButton->buttonClicked($sender,$param);

 (注:冒号“:”前面是挂接事件处理程序的方式,后面描述事件处理程序的定义形式以及如何调用这个事件处理程序)

原文如下:

TComponent class

TComponent is the base class for all PRADO components. TComponent implements the protocol of defining, using properties and events.

A property is defined by a getter method, and/or a setter method. Properties can be accessed in the way like accessing normal object members. Reading or writing a property will cause the invocation of the corresponding getter or setter method, e.g.,


1 $a=$this->Text; // equivalent to $a=$this->getText();
1 $this->Text='abc'; // equivalent to $this->setText('abc');
The signatures of getter and setter methods are as follows,

1 // getter, defines a readable property 'Text'
2 function getText() { ... }
3 // setter, defines a writable property 'Text', with $value being the value to be set to the property
4 function setText($value) { ... }
Property names are case-insensitive. It is recommended that they are written in the format of concatenated words, with the first letter of each word capitalized (e.g. DisplayMode, ItemStyle).

An event is defined by the presence of a method whose name starts with 'on'. The event name is the method name and is thus case-insensitive. An event can be attached with one or several methods (called event handlers). An event can be raised by calling raiseEvent method, upon which the attached event handlers will be invoked automatically in the order they are attached to the event. Event handlers must have the following signature,


1 function eventHandlerFuncName($sender,$param) { ... }
where $sender refers to the object who is responsible for the raising of the event, and $param refers to a structure that may contain event-specific information. To raise an event (assuming named as 'Click') of a component, use

1 $component->raiseEvent('OnClick');
To attach an event handler to an event, use one of the following ways,

1 $component->OnClick=$callback; // or $component->OnClick->add($callback);
2 $$component->attachEventHandler('OnClick',$callback);
The first two ways make use of the fact that $component->OnClick refers to the event handler list TList for the 'OnClick' event. The variable $callback contains the definition of the event handler that can be either a string referring to a global function name, or an array whose first element refers to an object and second element a method name/path that is reachable by the object, e.g.
  • 'buttonClicked' : buttonClicked($sender,$param);
  • array($object,'buttonClicked') : $object->buttonClicked($sender,$param);
  • array($object,'MainContent.SubmitButton.buttonClicked') : $object->MainContent->SubmitButton->buttonClicked($sender,$param);
posted @ 2011-04-21 16:58  thinksea  阅读(442)  评论(0编辑  收藏  举报