search
HomeBackend DevelopmentPHP TutorialYii 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) {
    ......
}

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();

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');

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();

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

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

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

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


Statement
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
使用Yii框架创建电影网站使用Yii框架创建电影网站Jun 21, 2023 am 09:04 AM

随着互联网的普及以及人们对电影的热爱,电影网站成为了一个受欢迎的网站类型。在创建一个电影网站时,一个好的框架是非常必要的。Yii框架是一个高性能的PHP框架,易于使用且具有出色的性能。在本文中,我们将探讨如何使用Yii框架创建一个电影网站。安装Yii框架在使用Yii框架之前,需要先安装框架。安装Yii框架非常简单,只需要在终端执行以下命令:composer

Yii框架简介:了解Yii的核心概念Yii框架简介:了解Yii的核心概念Jun 21, 2023 am 09:39 AM

Yii框架是一个高性能、高扩展性、高可维护性的PHP开发框架,在开发Web应用程序时具有很高的效率和可靠性。Yii框架的主要优点在于其独特的特性和开发方法,同时还集成了许多实用的工具和功能。Yii框架的核心概念MVC模式Yii采用了MVC(Model-View-Controller)模式,是一种将应用程序分为三个独立部分的模式,即业务逻辑处理模型、用户界面呈

为什么Yii框架比其他框架更好用?为什么Yii框架比其他框架更好用?Jun 21, 2023 am 10:30 AM

Yii框架是一个高性能、可扩展、安全的PHP框架。它是一个优秀的开发工具,能够让开发者快速高效地构建复杂的Web应用程序。以下是几个原因,让Yii框架比其他框架更好用。高性能Yii框架使用了一些先进的技术,例如,延迟加载(lazyloading)和自动加载机制(automaticclassloading),这使得Yii框架的性能高于许多其他框架。它还提

Yii框架中的ViewState:实现数据保护Yii框架中的ViewState:实现数据保护Jun 21, 2023 am 09:02 AM

ViewState是ASP.NET中的一种机制,用于保护页面的隐私数据。而在Yii框架中,ViewState同样也是实现页面数据保护的重要手段。在Web开发中,随着用户界面操作的复杂度增加,前端与后端之间的数据传输也愈发频繁。但是,不可避免的会有恶意用户通过网络抓包等手段截获数据。而未加保护的数据可能含有用户隐私、订单信息、财务数据等重要资料。因此,加密传输

Yii框架中的队列:高效地处理异步操作Yii框架中的队列:高效地处理异步操作Jun 21, 2023 am 10:13 AM

随着互联网的快速发展,应用程序对于处理大量并发请求和任务变得越来越重要。在这样的情况下,处理异步任务是必不可少的,因为这可以使应用程序更加高效,并更好地响应用户请求。Yii框架提供了一个方便的队列组件,使得处理异步操作更加容易和高效。在本篇文章中,我们将探讨Yii框架中队列的使用和优势。什么是队列队列是一种数据结构,用于处理数据的先进先出(FIFO)顺序。队

Yii框架中的分页机制:优化数据展示效果Yii框架中的分页机制:优化数据展示效果Jun 21, 2023 am 08:43 AM

在现今互联网时代,数据的处理和展示对于各种应用而言都是至关重要的。对于一些数据量较大的网站,其展示效果直接影响用户体验,而优秀的分页机制可以使得数据展示更加清晰,提高用户的使用体验。在本文中,我们将介绍Yii框架中的分页机制,并探讨如何通过优化分页机制来改进数据展示效果。Yii框架是一种基于PHP语言的高性能、适用于Web应用的开发框架。它提供

Yii框架中的扩展:使用外部库Yii框架中的扩展:使用外部库Jun 21, 2023 am 10:11 AM

Yii是一款优秀的PHP框架,它提供了很多丰富的功能和组件来加快Web应用程序的开发。其中一个非常重要的特性就是可以方便地使用外部库进行扩展。Yii框架中的扩展可以帮助我们快速完成许多常见的任务,例如操作数据库、缓存数据、发送邮件、验证表单等等。但是有时候,我们需要使用一些其他的PHP类库来完成特定的任务,例如调用第三方API、处理图片、生成PDF文件等等。

Yii框架中的身份认证与授权:保障应用程序的安全性Yii框架中的身份认证与授权:保障应用程序的安全性Jun 21, 2023 am 09:57 AM

在Web应用程序开发领域,身份认证和授权是保障应用程序安全性必不可少的两个环节,而Yii框架提供了完善的身份认证和授权机制,帮助开发者轻松实现这些功能,保障应用程序的安全性。一、身份认证1.1基础认证Yii框架中的基础认证机制采用HTTPBasic认证的方式实现。当用户在浏览器中访问需要认证的页面时,服务器会发送一个401Unauthorized响应,

See all articles

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 agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.