zoukankan      html  css  js  c++  java
  • yii2框架随笔18

    <?php
    
            /**
         * Sets value of an object property.
         * 设置对象的属性值。
         * Do not call this method directly as it is a PHP magic method that
         * will be implicitly called when executing `$object->property = $value;`.
         *
         * 魔术方法,实现 setter
         *
         * @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)) {
                // 对象存在 $setter 方法,就直接调用
                $this->$setter($value);
            } elseif (method_exists($this, 'get' . $name)) {
                // 如果存在 'get' . $name 方法,就认为该属性是只读的
                throw new InvalidCallException('Setting read-only property: ' . get_class($this) . '::' . $name);
            } else {
                // 否则认为该属性不存在
                throw new UnknownPropertyException('Setting unknown property: ' . get_class($this) . '::' . $name);
            }
        }
    
         /**
         * Checks if the named property is set (not null).
         * 检查属性是否被设置
         *
         * Do not call this method directly as it is a PHP magic method that
         * will be implicitly called when executing `isset($object->property)`.
         * 不要直接调用这个方法,因为它是一个PHP魔术方法呢,调用执行`isset($object->property)`。
         * Note that if the property is not defined, false will be returned.
         *
         * 魔术方法,实现 isset,基于 getter 实现,有 getter 方法的属性才算存在
         *
         * @param string $name the property name or the event name
         * @return boolean whether the named property is set (not null).
         */
        public function __isset($name)
            {
            $getter = 'get' . $name;
            if (method_exists($this, $getter)) {
                // 有 $getter 方法且获取的值不为 null,才认为该属性存在
                return $this->$getter() !== null;
            } else {
                return false;
            }
        }
        /**
         * Sets an object property to null.
         *
         * Do not call this method directly as it is a PHP magic method that
         * will be implicitly called when executing `unset($object->property)`.
         * 不要直接调用这个方法,因为它是一个PHP魔术方法呢,调用执行`isset($object->property)`。
         * Note that if the property is not defined, this method will do nothing.
         * If the property is read-only, it will throw an exception.
         *
         * 魔术方法,实现 unset,基于 setter 实现,有 setter 方法的属性才能 unset 掉
         *
         * @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
                $this->$setter(null);
            } elseif (method_exists($this, 'get' . $name)) {
                // 如果存在 'get' . $name 方法,就认为该属性是只读的
                throw new InvalidCallException('Unsetting read-only property: ' . get_class($this) . '::' . $name);
            }
        }
        /**
         * Calls the named method which is not a class method.
         *
         * 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.
         * 重写了__call()方法,在调用不存在的方法时会调用此方法,抛出异常
         * @param string $name the method name
         * @param array $params method parameters
         * @throws UnknownMethodException when calling unknown method
         * @return mixed the method return value
         */
        public function __call($name, $params)
        {
            throw new UnknownMethodException('Calling unknown method: ' . get_class($this) . 
        "::$name()");
        }
        /**
         * Returns a value indicating whether a property is defined.
         * 返回一个值指示是否定义属性。
         * A property is defined if:
         *
         * - the class has a getter or setter method associated with the specified name
         *   (in this case, property name is case-insensitive);
         * - the class has a member variable with the specified name (when `$checkVars` is true);
         *
         * 检查对象或类是否具有 $name 属性,如果 $checkVars 为 true,则不局限于是否有 getter/setter
         *
         * @param string $name the property name
         * @param boolean $checkVars whether to treat member variables as properties
         * @return boolean whether the property is defined
         * @see canGetProperty()
         * @see canSetProperty()
         */
        public function hasProperty($name, $checkVars = true)
        {
            return $this->canGetProperty($name, $checkVars) || $this->canSetProperty($name, false);
        }
        /**
         * Returns a value indicating whether a property can be read.
         * A property is readable if:
         *
         * - the class has a getter method associated with the specified name
         *   (in this case, property name is case-insensitive);
         * - the class has a member variable with the specified name (when `$checkVars` is true);
         *
         * 检查对象或类是否能够获取 $name 属性,如果 $checkVars 为 true,则不局限于是否有 getter
         *
         * @param string $name the property name
         * @param boolean $checkVars whether to treat member variables as properties
         * @return boolean whether the property can be read
         * @see canSetProperty()
  • 相关阅读:
    MySql 应用语句
    MySql 存储过程 退出
    MySql 存储过程 光标只循环一次
    MySql获取两个日期间的时间差
    VM VirtualBox 全屏模式 && 自动缩放模式 相互切换
    区分不同操作系统、编译器不同版本的宏
    debian下配置网络 安装无线网卡驱动 Broadcom BCMXX系列
    YII 主题设置
    Unix线程概念、控制原语、属性
    远程IPC种植木马
  • 原文地址:https://www.cnblogs.com/taokai/p/5450897.html
Copyright © 2011-2022 走看看