Yii2 源码学习--yii\base\Object

24 Oct 2016 Category: PHP

在Yii2中,所有的类的都集成于基类Object。Object对象通过几个php的魔术方法, 实现属性获取,设置,属性是否存在,属性是否可设置的方法。

1.构造函数

    public function __construct($config = [])
    {
        if (!empty($config)) {
            Yii::configure($this, $config);
        }
        $this->init();
    }

Object实现接口Configurable,因此可以通过构造函数通过传入config配置数组对 对象属性进行注入。对象创建之后,调用init方法执行用户设置的初始化。因此, 在Yii2中,如果我们要在某个类初始化执行相应的操作,应该重写init函数,而不 是构造函数。

2.获取属性

$value = $object->propert

对于对象中定义的public属性,直接返回。如果获取的属性没有定义为public,或者 没有定义,将执行魔术方法_get。

    public function __get($name)
    {
        $getter = 'get' . $name;
        if (method_exists($this, $getter)) {
            return $this->$getter();
        } elseif (method_exists($this, 'set' . $name)) {
            throw new InvalidCallException('Getting write-only property: ' . get_class($this) . '::' . $name);
        } else {
            throw new UnknownPropertyException('Getting unknown property: ' . get_class($this) . '::' . $name);
        }
    }

Object的魔术方法_get通过调用类中定义的get方法获取属性。如果对应的get方法不 存在,则根据对应set方法是否存在抛出属性不存在,或属性不可读的异常。

3.属性设置

    public function __set($name, $value)
    {
        $setter = 'set' . $name;
        if (method_exists($this, $setter)) {
            $this->$setter($value);
        } elseif (method_exists($this, 'get' . $name)) {
            throw new InvalidCallException('Setting read-only property: ' . get_class($this) . '::' . $name);
        } else {
            throw new UnknownPropertyException('Setting unknown property: ' . get_class($this) . '::' . $name);
        }
    }

Object的魔术方法_get通过调用类中定义的set方法获取属性。如果对应的set方法不 存在,则根据对应get方法是否存在抛出属性不存在,或属性不可设置的异常。

4.属性检测

    public function hasProperty($name, $checkVars = true)
    {
        return $this->canGetProperty($name, $checkVars) || $this->canSetProperty($name, false);
    }

    public function canGetProperty($name, $checkVars = true)
    {
        return method_exists($this, 'get' . $name) || $checkVars && property_exists($this, $name);
    }

    public function canSetProperty($name, $checkVars = true)
    {
        return method_exists($this, 'set' . $name) || $checkVars && property_exists($this, $name);
    }

通过hasProperty判断属性是否存在(包括只读,只写的属性)。如果$checkVars为true,则检查类的property。否 则只检查set方法,get方法是否存在。

4.属性是否设置

当调用isset($object->property),如果property不存在,将会调用。如果该属性未定义或者该属性值为null,将返 回false

    public function __isset($name)
    {
        $getter = 'get' . $name;
        if (method_exists($this, $getter)) {
            return $this->$getter() !== null;
        } else {
            return false;
        }
    }

5.unset属性

当调用unset($object->property),如果property不存在,将会调用。如果该属性未定义值抛出异常,否则调用set方法 将属性设置成null

    public function __unset($name)
    {
        $setter = 'set' . $name;
        if (method_exists($this, $setter)) {
            $this->$setter(null);
        } elseif (method_exists($this, 'get' . $name)) {
            throw new InvalidCallException('Unsetting read-only property: ' . get_class($this) . '::' . $name);
        }
    }

评论