yii2阅读随笔12

<?php

/**
     * Checks if a property value is null.
     *  检查属性值是否为空。
     * This method will check in the following order and act accordingly:
     * 此方法将在以下顺序检查并相应地采取行动:
     *  - a property defined by a setter: return whether the property value is null
     *   通过setter定义的属性:返回是否将属性值为空
     *  - a property of a behavior: return whether the property value is null

     *  属性的行为:返回属性值是否为空
     * Do not call this method directly as it is a PHP magic method that
     * will be implicitly called when executing `isset($component->property)`.
     *
     * 重写 Object 中的 isset 方法,添加对 behaviors 的处理,循环 behaviors,如果其中有相应的属性,就认为有
     *
     * @param string $name the property name or the event name
     * @return boolean whether the named property is null
     */
    public function __isset($name)
    {
        $getter = ‘get‘ . $name;
        if (method_exists($this, $getter)) {
            // 如果 $getter 方法存在,且不为 null,就返回 true
            return $this->$getter() !== null;
        } else {
            // behavior property
            $this->ensureBehaviors();
            // 循环所有的 behavior
            foreach ($this->_behaviors as $behavior) {
                if ($behavior->canGetProperty($name)) {
                    // 如果 behavior 中有 $name 属性,且不为 null,就返回 true
                    return $behavior->$name !== null;
                }
            }
        }
        return false;
    }
    /**
     * Sets a component property to be null.
     * 将组件属性设置为空。
     * This method will check in the following order and act accordingly:
     * 此方法将在以下顺序检查并相应地采取行动:
     *  - a property defined by a setter: set the property value to be null
     *  通过setter定义的属性:设置该属性值为空
     *  - a property of a behavior: set the property value to be null
     * 属性的行为:将属性值设为空
     * Do not call this method directly as it is a PHP magic method that
     * will be implicitly called when executing `unset($component->property)`.
     *
     * 重写 Object 中的 unset 方法,添加对 behaviors 的处理,循环 behaviors,如果其中有相应的属性,设置为空
     *
     * @param string $name the property name
     * @throws InvalidCallException if the property is read only.
     */
    public function __unset($name)
    {
        $setter = ‘set‘ . $name;
        if (method_exists($this, $setter)) {
            // 如果 $setter 方法存在,且不为 null,就返回 true
            $this->$setter(null);
            return;
        } else {
            // behavior property
            $this->ensureBehaviors();
            // 循环所有的 behavior
            foreach ($this->_behaviors as $behavior) {
                if ($behavior->canSetProperty($name)) {
                    // 如果 behavior 中有 $name 属性,就将该属性设为 null
                    $behavior->$name = null;
                    return;
                }
            }
        }
        throw new InvalidCallException(‘Unsetting an unknown or read-only property: ‘ . get_class($this) . ‘::‘ . $name);
    }
    /**
     * Calls the named method which is not a class method.
     * 称为非类方法的命名方法。
     * This method will check if any attached behavior has
     * the named method and will execute it if available.
     *
     * Do not call this method directly as it is a PHP magic method that
     * will be implicitly called when an unknown method is being invoked.
     *
     * 重写 Object 中的 call 方法,添加对 behaviors 的处理,循环 behaviors,如果其中有相应方法,就执行该 behavior 的方法
     *
     * @param string $name the method name
     * @param array $params method parameters
     * @return mixed the method return value
     * @throws UnknownMethodException when calling unknown method
     */
    public function __call($name, $params)
    {
        $this->ensureBehaviors();
        foreach ($this->_behaviors as $object) {
            if ($object->hasMethod($name)) {
                // behavior 中存在名为 $name 的方法,就执行它
                // call_user_func_array — 调用回调函数,并把一个数组参数作为回调函数的参数
                return call_user_func_array([$object, $name], $params);
            }
        }
        throw new UnknownMethodException(‘Calling unknown method: ‘ . get_class($this) . "::$name()");
    }
    /**
     * This method is called after the object is created by cloning an existing one.
     * It removes all behaviors because they are attached to the old object.
     *
     * 执行 clone 时,将其 _events 和 _behaviors 设置为空
     * 对象复制可以通过 clone 关键字来完成(如果可能,这将调用对象的 __clone() 方法)。对象中的 __clone() 方法不能被直接调用。
     * ~~~
     * $copy_of_object = clone $object;
     * ~~~
     * 当对象被复制后,PHP 5 会对对象的所有属性执行一个浅复制(shallow copy)。所有的引用属性 仍然会是一个指向原来的变量的引用。
     */
    public function __clone()
    {
        // 对象复制时,将它的 _events 设置为空数组,将 _behaviors 设置为 null
        $this->_events = [];
        $this->_behaviors = null;
    }
时间: 2024-07-30 05:18:30

yii2阅读随笔12的相关文章

yii2阅读随笔14

继续来看Event.php /** * Triggers a class-level event. * 触发类级别事件. * This method will cause invocation of event handlers that are attached to the named event * for the specified class and all its parent classes. * 触发某个类或者对象的某个事件 * @param string|object $cla

yii2阅读随笔13

下面我们来看一下Event.php <?php /** * @link http://www.yiiframework.com/ * @copyright Copyright (c) 2008 Yii Software LLC * @license http://www.yiiframework.com/license/ */ namespace yii\base; /** * Event is the base class for all event classes. * 事件是所有事件类的基

Android系统源码阅读(12):InputChannel的注册过程

Android系统源码阅读(12):InputChannel的注册过程 请对照AOSP版本:6.0.1_r50. InputManager可以获得输入事件并分发,Activity需要处理这些输入事件.那么,这两者之间如何建立的连接呢?这就需要InputChannel作为桥梁建立两者之间的通道. 1. ViewRootImpl创建InputChannel 这里ViewRoot类已经消失了,由ViewRootImpl替代.Activity在创建时会将自己的DecorView设置给对应的ViewRoo

Spring 中bean的scop 阅读随笔(记)

bean的scope scope用来声明容器中的对象所应该处的限定场景或者说该对象的存活时间,即容器在对象进入其相应的scope之前,生成并装配这些对象,在该对象不再处于这些scope的限定之后,容器通常会销毁这些对象. Spring容器最初提供了两种bean的scope类型:singleton和prototype,但发布2.0之后,又引入了另外三种scope类型,即request.session和global session类型.不过这三种类型有所限制,只能在Web应用中使用.也就是说,只有在

价值投资实战手册(唐朝) - 阅读随笔(一)

价值投资实战手册(唐朝) - 第一章 - 阅读随笔(一) 1.什么投资? 2.股票的本质 3.投资是持续中分的事 4.股票收益的来源 5.最简单的投资方法 6.普通投资者的道路 7.投资无需接盘侠 8.股权和其他投资品的对比 9.优秀企业的特征 原文地址:https://www.cnblogs.com/edwardsun/p/12232000.html

YII2框架阅读随笔

今天阅读的是vendor/yiisoft/yii2/web/User.php <?php /** * @link http://www.yiiframework.com/ * @copyright Copyright (c) 2008 Yii Software LLC * @license http://www.yiiframework.com/license/ */ namespace yii\web; //命名空间 use Yii; use yii\base\Component; use y

yii2框架随笔3

今天开始阅读vendor/yiisoft/yii2/base/Action.php <?php namespace yii\base;//命名空间 use Yii;//加载Yii文件夹下的Yii.php /** * Action is the base class for all controller action classes. * * Action provides a way to reuse action method code. An action method in an Acti

精华阅读第 12 期 | 最新 App Store 审核指南与10大被拒理由?

很多时候,我们对技术的追求是没有止境的,我们需要不断的学习,进步,再学习,再进步!本文系移动精英开发俱乐部的第12期文章推荐阅读整理,其中涉及到了 Android 数据库框架,架构设计中的循环引用,同时还阐述了如何在iOS中实现抽象类,以及什么是集群和分布式.同时,本文还分享了阿里巴巴的实践分享,如果你在开发一款APP,你也可以读一下<最新 App Store 审核指南与10大被拒理由?>.希望这些文章能对大家有所启发.文章系 ITOM 管理平台 OneAPM 审校整理: 1,Find Con

yii2框架随笔4

接下来我们继续了解Component.php 目录为:vendor/yiisoft/yii2/base/Component.php (接上次的代码) /** * Sets the value of a component property. *设置一个组件属性的值. * This method will check in the following order and act accordingly: *这种方法将检查以下顺序并采取相应的行动: * - a property defined by