<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\base;
use Yii;
use yii\helpers\StringHelper;
/**
* 该类为 property event behavior 的基类
*
* 该组件给 event 和 behavior 的特性 , 以及实现于 在父类中的 property 特性的
*
* 事件是一种将自定义的代码 "注入" 到特定的位置的现有代码的方法 。 例如,注释对象可以触发
* 用户添加评论时的“添加”事件。我们可以编写自定义代码并将其附加到此事件,以便当事件发生时
* 是触发(即注释将被添加),我们的自定义代码将被执行。
*
* 事件由一个名称标识,该名称在其定义的类中应该是唯一的。事件名是*区分大小写的*。
*
* 一个或多个PHP回调,称为*事件处理程序*,可以附加到一个事件。您可以调用 trigger() to
* 引发事件。当引发事件时,将按事件处理程序的顺序自动调用事件处理程序
*
* 要将事件处理程序附加到事件,调用 on() :
*
* ```php
* $post->on('update', function ($event) {
* // send email notification
* });
* ```
*
* 在上面,一个匿名函数被附加到post的“update”事件。你可以附加以下类型的事件处理程序:
*
* - 匿名函数:`function ($event){…}'
* - 对象方法:' [$object, 'handleAdd'] '
* - 静态类方法:' ['Page', 'handleAdd'] '
* - 全局函数:“handleAdd '
*
* 事件处理程序的签名应该如下:
*
* ```php
* function foo($event)
* ```
*
* 其中' $event '是一个[[event]]对象,它包含与事件相关的参数。
*
* 在使用配置数组配置组件时,还可以将处理程序附加到事件。语法如下:
*
* ```php
* [
* 'on add' => function ($event) { ... }
* ]
* ```
*
* 其中' on add '表示将事件附加到' add '事件。
*
* 有时,您可能希望在将额外数据附加到事件时将其与事件处理程序关联起来然后在调用处理程序时访问它。你可以这样做
*
* ```php
* $post->on('update', function ($event) {
* // the data can be accessed via $event->data
* }, $data);
* ```
*
* 一个行为是 behavior 或者是它的一个子类的一个实例。组件可以附加一个或多个行为。
* 当行为被附加到组件时,可以通过组件直接访问其公共属性和方法,就好像组件拥有这些属性和方法一样。
*
* 要将行为附加到组件,请在 behaviors() 中声明 ,或者显示的调用 attachBehavior 。行为 在 behaviors() 中声明的自动附加到相应的组件
*
* 在用配置数组配置组件时,还可以将行为附加到组件。语法如下:
*
* ```php
* [
* 'as tree' => [
* 'class' => 'Tree',
* ],
* ]
* ```
*
* 其中 as tree 表示附加一个名为tree 的行为 ,数组将被传递给 \Yii::createObject() 创建对应的对象
*
* 有关组件的详细信息和使用信息,请参阅[关于组件的指导文章](guide:concept-components).
*
* @property Behavior[] $behaviors 附加到此组件的行为列表。此属性是只读的。
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class Component extends BaseObject
{
/**
* @var array 附加的事件处理程序 (事件名称=>处理程序)
*/
private $_events = [];
/**
* @var array 为通配符模式附加的事件处理程序(事件名通配符=>处理程序)
* @since 2.0.14
*/
private $_eventWildcards = [];
/**
* @var Behavior[]|null 附加的行为(行为名=>行为)。这是' null '时,没有初始化。
*/
private $_behaviors;
/**
* 返回组件属性的值。
*
* 该方法将按照以下顺序进行检查并采取相应的行动:
*
* - 由getter定义的属性:返回getter结果
* - 行为的属性:返回行为属性值
*
* Do not call this method directly as it is a PHP magic method that
* will be implicitly called when executing `$value = $component->property;`.
* @param string $name the property name
* @return mixed the property value or the value of a behavior's property
* @throws UnknownPropertyException if the property is not defined
* @throws InvalidCallException if the property is write-only.
* @see __set()
*/
public function __get($name)
{
$getter = 'get' . $name;
if (method_exists($this, $getter)) {
// read property, e.g. getName()
return $this->$getter();
}
// behavior property
$this->ensureBehaviors();
foreach ($this->_behaviors as $behavior) {
if ($behavior->canGetProperty($name)) {
return $behavior->$name;
}
}
if (method_exists($this, 'set' . $name)) {
throw new InvalidCallException('Getting write-only property: ' . get_class($this) . '::' . $name);
}
throw new UnknownPropertyException('Getting unknown property: ' . get_class($this) . '::' . $name);
}
/**
* 设置组件属性的值。
*
* 该方法将按照以下顺序进行检查并采取相应的行动:
*
* - 由setter定义的属性:设置属性值
* - “on xyz”格式的事件:将处理程序附加到事件“xyz”
* - “作为xyz”格式的行为:附加名为“xyz”的行为
* - 行为的属性:设置行为属性值
*
* Do not call this method directly as it is a PHP magic method that
* will be implicitly called when executing `$component->property = $value;`.
* @param string $name the property name or the event name
* @param mixed $value the property value
* @throws UnknownPropertyException if the property is not defined
* @throws InvalidCallException if the property is read-only.
* @see __get()
*/
public function __set($name, $value)
{
$setter = 'set' . $name;
if (method_exists($this, $setter)) {
//
把杯子里面的水清空,从零开始学习编程。第五节 yii/base/Component.php
本文深入探讨了在编程学习过程中如何从基础出发,通过研究yii/base/Component.php源码,理解PHP框架Yii的工作原理。通过实例解析关键类和方法,助你构建扎实的编程基础。
摘要由CSDN通过智能技术生成