Table of Contents
1. Component properties
2. Component events
Home Backend Development PHP Tutorial Yii Framework Official Guide Series 10 - Basics: Components

Yii Framework Official Guide Series 10 - Basics: Components

Feb 11, 2017 am 09:41 AM



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 属性
Copy after login

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;
}
Copy after login

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);
}
Copy after login

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;
Copy after login

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)
{
    ......
}
Copy after login

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) {
    ......
}
Copy after login

If we now call

onClicked(), onClicked The event will be triggered (in onClicked()), and the attached event handler will be automatically called.

An event can be bound to multiple handles. When an event fires, these handlers will be executed in the order in which they were bound to the event. If the handler decides to prevent subsequent handlers from being executed, it can set $event->handled to true.

3. Component Behavior

Starting from version 1.0.2, the component has added support for mixin and can bind one or more behaviors.

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();
Copy after login

The bound behavior can be like in a component Accessed like normal properties. For example, if a behavior named

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');
Copy after login

Behavior can be temporarily disabled, at which time its method will become invalid in the component. For example :


$component->disableBehavior($name);
// 下面的代码将抛出一个异常
$component->test();
$component->enableBehavior($name);
// 现在就可以使用了
$component->test();
Copy after login

两个同名行为绑定到同一个组件下是有可能的。在这种情况下,先绑定的行为则拥有优先权。

当和 events, 一起使用时,行为会更加强大。当行为被绑定到组件时,行为里的一些方法就可以绑定到组件的一些事件上了. 这样一来,行为就可以观察或者改变组件的常规执行流程。

自版本 1.1.0 开始,一个行为的属性也可以通过绑定到的组件来访问。 这些属性包含公共成员变量以及通过 getters 和/或 setters 方式设置的属性。 例如, 若一个行为有一个 xyz 的属性,此行为被绑定到组件 $a,然后我们可以使用表达式 $a->xyz 访问此行为的属性。

以上就是Yii框架官方指南系列10——基础知识:组件的内容,更多相关内容请关注PHP中文网(www.php.cn)!


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to install the Windows 10 old version component DirectPlay How to install the Windows 10 old version component DirectPlay Dec 28, 2023 pm 03:43 PM

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

How to implement calendar component using Vue? How to implement calendar component using Vue? Jun 25, 2023 pm 01:28 PM

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,

How to use Yii framework in PHP How to use Yii framework in PHP Jun 27, 2023 pm 07:00 PM

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,

Angular components and their display properties: understanding non-block default values Angular components and their display properties: understanding non-block default values Mar 15, 2024 pm 04:51 PM

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.

How to open the settings of the old version of win10 components How to open the settings of the old version of win10 components Dec 22, 2023 am 08:45 AM

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: paging component development Vue component practice: paging component development Nov 24, 2023 am 08:56 AM

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: implementation method of progress bar component Vue component development: implementation method of progress bar component Nov 24, 2023 am 08:56 AM

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 Yii framework Steps to implement web page caching and page chunking using Yii framework Jul 30, 2023 am 09:22 AM

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

See all articles