所有控制器action的基类yii\base\Action.php

 namespace yii\base;//定义的命名空间

 use Yii //使用的命名空间

 class Action extends Component //继承了组建类
 {
          //定义属性action的id

     public $id;

     public $controller; //定义拥有该action的控制器属性

     public function __construct($id, $controller, $config = [])//构造函数,用于初始化id和controller属性
     {
         $this->id = $id;
         $this->controller = $controller;
         parent::__construct($config);
     }

       //通过调用控制器中的同名方法,
     public function getUniqueId()
     {
         return $this->controller->getUniqueId() . '/' . $this->id;
     }
   //用指定的参数运行此操作
     public function runWithParams($params)
     {
         if (!method_exists($this, 'run')) {//如果类中的run方法是不存在
             throw new InvalidConfigException(get_class($this) . ' must define a "run()" method.');//抛出异常
         }
         $args = $this->controller->bindActionParams($this, $params);//控制器的方法,绑定参数
         Yii::trace('Running action: ' . get_class($this) . '::run()', __METHOD__);//输出trace信息
         if (Yii::$app->requestedParams === null) {  //如果Yii::$app的属性requestedParams(操作请求的参数)为空
             Yii::$app->requestedParams = $args;//该参数的值为上面绑定参数的返回值
         }
         if ($this->beforeRun()) {//beforRun方法,默认返回true,意思为run前
             $result = call_user_func_array([$this, 'run'], $args);//把$args作为参数来调用当前控制器的run方法
             $this->afterRun();//空方法

             return $result;//返回函数的调用结果,如出错,返回false
         } else {
             return null;//否则返回空
         }
     }

     protected function beforeRun()//该方法在run之前执行,调用时重写该方法,如果返回为false,则run方法不执行,runwithparams方法返回空
     {
         return true;
     }

     //该方法在run方法后执行,用于处理后续操作
     protected function afterRun()
     {
     }
 }

component.php组件类

class Component extends Object  //继承了object类
{

    private $_events = [];//私有的events属性

    private $_behaviors;//私有的behaviors属性

public function __get($name)//__get魔术方法,用于返回组组件的属性值
    {
        $getter = 'get' . $name;//通过连字符构建该属性的get方法名
        if (method_exists($this, $getter)) {//如果该方法存在
            // read property, e.g. getName()
            return $this->$getter();//调用该方法获取属性
        } else {
            // behavior property
            $this->ensureBehaviors();//调用ensureBehaviors()方法确定该行为类中定义了该属性
            foreach ($this->_behaviors as $behavior) {
                if ($behavior->canGetProperty($name)) {//如果该行为类可以获取属性
                    return $behavior->$name;//返回该行为类的属性值
                }
            }
        }
        if (method_exists($this, 'set' . $name)) {//如果该属性名的set方法存在抛出异常
            throw new InvalidCallException('Getting write-only property: ' . get_class($this) . '::' . $name);
        } else {//否则抛出异常,未知的属性
            throw new UnknownPropertyException('Getting unknown property: ' . get_class($this) . '::' . $name);
        }
    }

component的__set方法

  public function __set($name, $value)
     {
         $setter = 'set' . $name;//在属性名前面加set构建set方法
         if (method_exists($this, $setter)) {//如果构建的方法存在
             // 调用该方法设置属性值
             $this->$setter($value);

             return;
         } elseif (strncmp($name, 'on ', 3) === 0) {//如果方法不存在,且属性名前三个字符为on+空格
             // on event: attach event handler
             $this->on(trim(substr($name, 3)), $value);//调用on方法将事件处理程序附加到事件

             return;
         } elseif (strncmp($name, 'as ', 3) === 0) {//否则,如果属性名的前三个字符为as+空格
             // as behavior: attach behavior
             $name = trim(substr($name, 3));//截取as后面的字符
             $this->attachBehavior($name, $value instanceof Behavior ? $value : Yii::createObject($value));//嗲用attachBehavior添加一个行为到组件

             return;
         } else {//否则
             // behavior property
             $this->ensureBehaviors();//调用ensureBehaviors()方法确定该行为类中定义了该属性
             foreach ($this->_behaviors as $behavior) {
                 if ($behavior->canSetProperty($name)) {//调用方法判断该属性是否口试被赋值
                     $behavior->$name = $value;//给该行为类的属性赋值

                     return;
                 }
             }
         }
         if (method_exists($this, 'get' . $name)) {//如果该属性的get方法存在,抛出异常
             throw new InvalidCallException('Setting read-only property: ' . get_class($this) . '::' . $name);
         } else {//否则抛出异常,未知的属性
             throw new UnknownPropertyException('Setting unknown property: ' . get_class($this) . '::' . $name);
         }
     }

Yii源码阅读笔记(四)的更多相关文章

  1. Yii源码阅读笔记(一)

    今天开始阅读yii2的源码,想深入了解一下yii框架的工作原理,同时学习一下优秀的编码规范和风格.在此记录一下阅读中的小心得. 每个框架都有一个入口文件,首先从入口文件开始,yii2的入口文件位于we ...

  2. Werkzeug源码阅读笔记(四)

    今天主要讲一下werkzeug中的routing模块.这个模块是werkzeug中的重点模块,Flask中的路由相关的操作使用的都是这个模块 routing模块的用法 在讲解模块的源码之前,先讲讲这个 ...

  3. Yii源码阅读笔记(三十四)

    Instance类, 表示依赖注入容器或服务定位器中对某一个对象的引用 namespace yii\di; use Yii; use yii\base\InvalidConfigException; ...

  4. Yii源码阅读笔记(二十四)

    Module类中获取子模块,注册子模块,实例化控制器,根据路由运行指定控制器方法的注释: /** * Retrieves the child module of the specified ID. * ...

  5. Yii源码阅读笔记(十四)

    Model类,集中整个应用的数据和业务逻辑——场景.属性和标签: /** * Returns a list of scenarios and the corresponding active attr ...

  6. Yii源码阅读笔记(二)

    接下来阅读BaseYii.php vendor/yiisoft/yii2/BaseYii.php—— namespace yii; use yii\base\InvalidConfigExceptio ...

  7. Yii源码阅读笔记(八)

    前面阅读了Yii2的两个基本类Object和Component,了解了Yii的三个重要概念属性.事件.行为,下面开始阅读Event类,Event类是所有事件类的基类: <?php /** * @ ...

  8. Yii源码阅读笔记(三)

    接着上次的继续阅读BaseYii.php vendor/yiisoft/yii2/BaseYii.php—— public static function getRootAlias($alias)// ...

  9. Yii源码阅读笔记(三十五)

    Container,用于动态地创建.注入依赖单元,映射依赖关系等功能,减少了许多代码量,降低代码耦合程度,提高项目的可维护性. namespace yii\di; use ReflectionClas ...

随机推荐

  1. CentOS 6.5 下安装 Elasticsearch 5

    安装最新的 Elasticsearch 5 需要Java 8.所有先要确定环境中是否有Java 8.如果没有则需要安装. 1. 安装Java 8 首先使用 yum list installed | g ...

  2. 64位环境中使用SQL查询excel的方式解决

    --64位环境中使用SQL查询excel的方式 环境: OS:Windows Server 2008 R2 Enterprise MSSQL:Microsoft SQL Server 2008 R2 ...

  3. (转载)数据库表设计-水电费缴费系统(oracle)

    水电缴费管理系统数据表设计 SQL建表脚本: 1 --建表 2 --管理人员表 admin 3 create table admin( 4 admin_id varchar2(3) not null, ...

  4. Centos 6.4 32位 gcc 升级(已验证)

    具体需要升级成什么版本自行下载https://gcc.gnu.org/ 本文升级为4.8.5 1.下载编译所需依赖库 cd gcc-4.8.5 ./contrib/download_prerequis ...

  5. web应用配置

    tomcat 的 server.html 配置文件 加在</Host>之上 <Context path=”/itcast” docBase=”c:\news” /> path虚 ...

  6. web service介绍

    WEB SEVICE平台 web Service 三种基本元素: SOAP .WSDL .UDDI 什么是SOAP:       XML+HTTP 基本的Web Service平台 SOAP 简易对象 ...

  7. ember.js:使用笔记7 页面中插入效果

    在某些情况下,我们需要根据数据生成某些效果:由于每个模版的controller可能不同,在不同页面之间跳转可能会无法随即更新的问题. controller: 直接使用标签:{{}},适用于在子项目内切 ...

  8. .NET 4.0中的泛型的协变和逆变

    转自:http://www.cnblogs.com/jingzhongliumei/archive/2012/07/02/2573149.html 先做点准备工作,定义两个类:Animal类和其子类D ...

  9. Sprint第一个冲刺(第十一天)

    一.Sprint介绍 修改登录信息界面(修改用户名.密码.邮箱.电话.年龄),且同步到云端:修改Item布局:增添设置页. 实验截图: 任务进度: 二.Sprint周期 看板: 燃尽图:

  10. HDU 5212 Code

    筛法. 统计所有 [数] 的所有 [倍数] 的 [数] 的个数,即 i 的所有倍数 i, 2i, 3i, 4i...个数为 dp[i], 则所有 倍数两两结合共有 dp[i] * dp[i] 个. 此 ...