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.

  • 相关阅读:
    hdu 3333 树状数组+离线处理
    poj 2352 树状数组 OR Treap
    hdu 1698 线段树
    【概率dp】D. Card Collector
    【分段哈希】H. Paint the Wall
    【置换】G. Poker 2.0
    【概率dp】C. Race to 1 Again
    【dp】D. Caesar's Legions
    【并查集】F.find the most comfortable road
    【算法系列学习】连续邮资问题
  • 原文地址:https://www.cnblogs.com/thinksasa/p/3848574.html
Copyright © 2011-2022 走看看