zoukankan      html  css  js  c++  java
  • yii源码一 -- CComponent

    CComponent:

    path:framework/base/CComponent.php

    overview:This file contains the foundation classes for component-based and event-driven programming.
                  (包含基于组件和事件驱动编程的基础类,是所有components的基类)

    1.property定义

    $a=$component->text;     // equivalent to $a=$component->getText();
    $component->text='abc';  // equivalent to $component->setText('abc');

    2.event事件定义
    1)事件句柄function handlerName($event) {}

    function eventHandler($event) { ... }

      添加事件句柄到事件:(也可用attachEventHandler)

    $component->onClick=$callback;    // or $component->onClick->add($callback);

    2)事件:事件名称必须以on开头

    3)启用事件raiseEvent()

    public function onClick($event){
          $this->raiseEvent('onClick',$event);
    }

    3.behaviors行为
          行为是附加到组件components上的实例。可以添加(attachBehavior),拆卸(detachBehavior),也可以启用(enableBehavior)、禁用(disableBehavior).

    注意:1.属性名(property)和事件名(event)大小写敏感。

    源码分析:

    1.重写:
      重写__get()和__set()两个魔术方法,便于可以直接调用和设置模型的私有属性;

         重写__isset()和__unset()两个魔术方法,便于判断模型的私有属性是否设置和清空属性值;

         重写__call()魔术方法,便于调用不存在的或者私有的类方法。

    2.事件的有无判断和启用;

    3.行为Behavior的添加、删除、启用、禁用;

    4.Evaluates a PHP expression。

    重点分析魔术方法:__set():

         比如实例化后$userModel=new User();然后提交表单的时候$userModel->attributs = $_POST;(关键在这步).

        public function __set($name,$value)
        {    
            /**
             * 当执行$userModel->attributes=$_POST时:
             * 1.因为没有attributes属性,所以会调用此魔术方法__set();
             * 2.传进来的值是:$name="setattributes",
                          $value = array(3) {
                                              ["username"]=>
                                              string(5) "admin"
                                              ["password"]=>
                                              string(9) "admin"
                                              ["rememberMe"]=>
                                              string(1) "0"
                                            }
             * 3.'set'.$name返回的值是:setattributes;
             * 4.因此当method_exists($this,$setter)存在这个方法的时候会调用
             *   $this->setattributes()方法。而setattributes()在/base/CModel.php
             *   里面进行了处理.
             */
            $setter='set'.$name;//得到$setter='setattributes';
            if(method_exists($this,$setter))
                return $this->$setter($value);//调用$this->setattributes($value);


    类:CEvent
    overview:CEvent is the base class for all event classes.

    类:CEnumerable
    overview:CEnumerable is the base class for all enumerable types.
    用法:

    class TextAlign extends CEnumerable{
        const Left='Left';
        const Right='Right';
    }

    调用:TextAlign::Left。

    tips:The constant name must be the same as the constant value.

  • 相关阅读:
    CJSon使用
    mqtt学习-3 编译运行测试
    mqtt学习-2 创建c vs项目
    mqtt学习-1 Mqtt服务器搭建
    Linux c 开发-5 使用VsCode远程调试Linux程序
    Layui数据表格之获取表格中所有的数据方法
    layui 给数据表格加序号的方法
    Layui关闭弹出层并刷新父页面,父页面向子页面传值
    MUI中小数点的数字输入框,步进step为小数时的需求和浮点数的精确问题
    MUI-numbox(数字输入框),最小值、最大值、步长、获取值、设置值、重定义
  • 原文地址:https://www.cnblogs.com/thinksasa/p/3848574.html
Copyright © 2011-2022 走看看