Yii Framework Official Guide Series 10 - Basics: Components
Yii applications are built on components. Components are instances of CComponent or its subclasses. Using a component mainly involves accessing its properties and when to trigger or handle it. The base class CComponent specifies how properties and events are defined.
1. Component properties
The properties of a component are like the public member variables of an object. It is readable and writable. For example:
$width=$component->textWidth; // 获取 textWidth 属性 $component->enableCaching=true; // 设置 enableCaching 属性
To define a component property, we only need to define a public member variable in the component class . A more flexible way is to define its getter and setter methods, for example:
public function getTextWidth() { return $this->_textWidth; } public function setTextWidth($value) { $this->_textWidth=$value; }
The above code defines a writable The attribute name is textWidth
(the name is case-insensitive). When reading properties, getTextWidth()
will be called, and its return value becomes the property value; similarly, when writing properties, setTextWidth()
will be called. If the setter method is not defined, the property will be read-only and an exception will be thrown if written to it. One benefit of defining a property using getter and setter methods is that additional logic can be performed (e.g., performing validations, triggering events) when the property is read or written.
Note: There is a subtle difference between properties defined through getters/setters and class member variables. The former's name is case-insensitive, while the latter's is case-sensitive.
2. Component events
Component events are special properties that use methods called event handlers
as its value. Attaching (assigning) a method to an event will cause the method to be automatically called when the event is raised. Therefore, the behavior of a component may be modified in a way that was unforeseen during component development.
Component events are defined using naming methods starting with
on. Like properties defined through getter/setter methods, event names are case-insensitive. The following code defines an onClicked
event:
public function onClicked($event) { $this->raiseEvent('onClicked', $event); }
used as event parameters here $event
is an instance of CEvent or one of its subclasses.
We can attach a method to this event as follows:
$component->onClicked=$callback;
Here $callback
points to a valid PHP callback. It can be a global function or a method in a class. If it is the latter, it must be provided as an array: array($object,'methodName')
.
The structure of the event handler is as follows:
function methodName($event) { ...... }
The $event
here is the parameter describing the event (it comes from the raiseEvent()
call) . $event
The parameter is an instance of CEvent or its subclass. At the very least, it contains information about who triggered the event.
Starting from version 1.0.10, the event handler can also be an anonymous function supported by PHP 5.3 or later. For example,
##
$component->onClicked=function($event) { ...... }
onClicked(),
onClicked The event will be triggered (in
onClicked()), and the attached event handler will be automatically called.
Behavior is an object whose methods can be implemented by the components it is bound to by collecting functions inherited, rather than specialized inheritance (i.e. ordinary class inheritance) ). A component can implement the binding of multiple behaviors through 'multiple inheritance'.
The behavior class must implement the IBehavior interface. Most behaviors can be inherited from CBehavior . If a behavior needs to be bound to a model, it can also inherit from CModelBehavior or CActiveRecordBehavior which implement binding features specifically for the model.
To use a behavior, it must first be bound to a component by calling this behavior's attach() method. Then we can call this behavior method through the component:
// $name 在组件中实现了对行为的唯一识别 $component->attachBehavior($name,$behavior); // test() 是行为中的方法。 $component->test();
tree is bound to a component, we can get a reference to this behavior through the following code.
$behavior=$component->tree; // 等于下行代码: // $behavior=$component->asa('tree');
$component->disableBehavior($name); // 下面的代码将抛出一个异常 $component->test(); $component->enableBehavior($name); // 现在就可以使用了 $component->test();
两个同名行为绑定到同一个组件下是有可能的。在这种情况下,先绑定的行为则拥有优先权。
当和 events, 一起使用时,行为会更加强大。当行为被绑定到组件时,行为里的一些方法就可以绑定到组件的一些事件上了. 这样一来,行为就可以观察或者改变组件的常规执行流程。
自版本 1.1.0 开始,一个行为的属性也可以通过绑定到的组件来访问。 这些属性包含公共成员变量以及通过 getters 和/或 setters 方式设置的属性。 例如, 若一个行为有一个 xyz 的属性,此行为被绑定到组件 $a,然后我们可以使用表达式 $a->xyz
访问此行为的属性。
以上就是Yii框架官方指南系列10——基础知识:组件的内容,更多相关内容请关注PHP中文网(www.php.cn)!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Many users always encounter some problems when playing some games on win10, such as screen freezes and blurred screens. At this time, we can solve the problem by turning on the directplay function, and the operation method of the function is also Very simple. How to install directplay, the old component of win10 1. Enter "Control Panel" in the search box and open it 2. Select large icons as the viewing method 3. Find "Programs and Features" 4. Click on the left to enable or turn off win functions 5. Select the old version here Just check the box

Vue is a very popular front-end framework. It provides many tools and functions, such as componentization, data binding, event handling, etc., which can help developers build efficient, flexible and easy-to-maintain Web applications. In this article, I will introduce how to implement a calendar component using Vue. 1. Requirements analysis First, we need to analyze the requirements of this calendar component. A basic calendar should have the following functions: display the calendar page of the current month; support switching to the previous month or next month; support clicking on a certain day,

With the rapid development of web applications, modern web development has become an important skill. Many frameworks and tools are available for developing efficient web applications, among which the Yii framework is a very popular framework. Yii is a high-performance, component-based PHP framework that uses the latest design patterns and technologies, provides powerful tools and components, and is ideal for building complex web applications. In this article, we will discuss how to use Yii framework to build web applications. Install Yii framework first,

The default display behavior for components in the Angular framework is not for block-level elements. This design choice promotes encapsulation of component styles and encourages developers to consciously define how each component is displayed. By explicitly setting the CSS property display, the display of Angular components can be fully controlled to achieve the desired layout and responsiveness.

Win10 old version components need to be turned on by users themselves in the settings, because many components are usually closed by default. First we need to enter the settings. The operation is very simple. Just follow the steps below. Where are the win10 old version components? Open 1. Click Start, then click "Win System" 2. Click to enter the Control Panel 3. Then click the program below 4. Click "Enable or turn off Win functions" 5. Here you can choose what you want to open

Vue component practice: Introduction to paging component development In web applications, the paging function is an essential component. A good paging component should be simple and clear in presentation, rich in functions, and easy to integrate and use. In this article, we will introduce how to use the Vue.js framework to develop a highly customizable paging component. We will explain in detail how to develop using Vue components through code examples. Technology stack Vue.js2.xJavaScript (ES6) HTML5 and CSS3 development environment

Vue component development: Progress bar component implementation method Preface: In Web development, the progress bar is a common UI component, often used to display the progress of operations in scenarios such as data requests, file uploads, and form submissions. In Vue.js, we can easily implement a progress bar component by customizing components. This article will introduce an implementation method and provide specific code examples. I hope it will be helpful to Vue.js beginners. Component structure and style First, we need to define the basic structure and style of the progress bar component.

Steps to implement web page caching and page chunking using the Yii framework Introduction: During the web development process, in order to improve the performance and user experience of the website, it is often necessary to cache and chunk the page. The Yii framework provides powerful caching and layout functions, which can help developers quickly implement web page caching and page chunking. This article will introduce how to use the Yii framework to implement web page caching and page chunking. 1. Turn on web page caching. In the Yii framework, web page caching can be turned on through the configuration file. Open the main configuration file co
