把杯子裏面的水清空,從零開始學習編程。第五節 yii/base/BaseObject.php

原本是想讀 /yii/di/Container 類但是發現,直接讀這個類,還是有點不懂所以我就開始從它的繼承的父級開始 。

<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace yii\base;

use Yii;

/**
 * BaseObject是實現*property*特性的基類。
 *
 * 屬性是由getter方法定義的(例如' getLabel ')和/或setter方法(例如“setLabel”)。例如,
 * 以下getter和setter方法定義了一個名爲' label '的屬性:
 *
 * ```php
 * private $_label;
 *
 * public function getLabel()
 * {
 *     return $this->_label;
 * }
 *
 * public function setLabel($value)
 * {
 *     $this->_label = $value;
 * }
 * ```
 *
 * 屬性名不區分大小寫。
 *
 * 屬性可以像對象的成員變量一樣被訪問。讀取或寫入屬性將導致調用相應的getter或setter方法。例如,
 *
 * ```php
 * // equivalent to $label = $object->getLabel();
 * $label = $object->label;
 * // equivalent to $object->setLabel('abc');
 * $object->label = 'abc';
 * ```
 *
 * 如果一個屬性只有一個getter方法而沒有setter方法,那麼它被認爲是“只讀的”。在這種情況下,嘗試修改屬性值將導致異常。
 *
 * 可以調用 hasProperty() , canGetProperty() , canSetProperty() 來檢查屬性的存在
 *
 * 除了屬性特性外,BaseObject還引入了一個重要的對象初始化生命週期.
 * 特別地,創建一個新的BaseObject實例或它的派生類將依次涉及以下生命週期:
 *
 * 1. 調用類構造函數
 * 2. 對象屬性根據給定的配置初始化;
 * 3. 調用 init() 方法
 *
 * 在上面的例子中,第2步和第3步都出現在類構造函數的末尾
 * 建議您在' init() '方法中執行對象初始化,因爲在這個階段,對象配置已經被應用。
 *
 * 爲了確保上述生命週期,如果BaseObject的一個子類需要重寫構造函數,具體做法如下:
 *
 * ```php
 * public function __construct($param1, $param2, ..., $config = [])
 * {
 *     ...
 *     parent::__construct($config);
 * }
 * ```
 *
 * 也就是說,應該將' $config '參數(默認值爲'[]')聲明爲構造函數的最後一個參數,並在構造函數的末尾調用父實現。
 *
 * @author Qiang Xue <[email protected]>
 * @since 2.0.13
 */
class BaseObject implements Configurable
{
    /**
     * 返回該類的完全限定名。
     * @return string 該類的完全限定名。
     * @deprecated since 2.0.14. 在PHP >=5.5中,使用'::class '代替。
     */
    public static function className()
    {
        return get_called_class();
    }

    /**
     * 構造函數
     *
     * 默認的實現做兩件事:
     *
     * - 使用給定的配置' $config '初始化對象。
     * - 調用 init()
     *
     * 如果在子類中重寫此方法,建議這樣做
     *
     * - 構造函數的最後一個參數是一個配置數組,如這裏的' $config '。
     * - 在構造函數的末尾調用父實現。
     *
     * @param array $config 將用於初始化對象屬性的名稱-值對
     */
    public function __construct($config = [])
    {
        if (!empty($config)) {
            Yii::configure($this, $config);
        }
        $this->init();
    }

    /**
     * 初始化對象。
     * 使用給定的配置初始化對象之後,在構造函數的末尾調用此方法。
     */
    public function init()
    {
    }

    /**
     * 返回對象屬性的值。
     *
     * 不要直接調用這個方法,因爲它是一個PHP魔法方法,在執行' $value = $object->property; '時會隱式地調用它。
     *
     * @param string $name 屬性名
     * @return mixed 屬性值
     * @throws UnknownPropertyException if the property is not defined
     * @throws InvalidCallException if the property is write-only
     * @see __set()
     */
    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);
        }

        throw new UnknownPropertyException('Getting unknown property: ' . get_class($this) . '::' . $name);
    }

    /**
     * 設置對象屬性的值
     *
     * 不要直接調用這個方法,因爲它是一個PHP魔法方法
     * 將在執行“$object->property = $value;”時隱式調用。
     *
     * @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)) {
            $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);
        }
    }

    /**
     * 檢查屬性是否已設置,即是否已定義,是否不爲空。
     *
     * 不要直接調用這個方法,因爲它是一個PHP魔法方法
     * 將在執行“isset($object->屬性)”時隱式調用。
     *
     * 注意,如果屬性沒有定義,將返回false。
     *
     * @param string $name the property name or the event name
     * @return bool whether the named property is set (not null).
     * @see https://secure.php.net/manual/en/function.isset.php
     */
    public function __isset($name)
    {
        $getter = 'get' . $name;
        if (method_exists($this, $getter)) {
            return $this->$getter() !== null;
        }

        return false;
    }

    /**
     * 將對象屬性設置爲null。
     *
     * Do not call this method directly as it is a PHP magic method that
     * will be implicitly called when executing `unset($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.
     * @param string $name the property name
     * @throws InvalidCallException if the property is read only.
     * @see https://secure.php.net/manual/en/function.unset.php
     */
    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);
        }
    }

    /**
     * 調用命名的方法,而不是類方法。
     *
     * 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.
     * @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()");
    }

    /**
     * 返回一個值,該值指示是否定義了屬性
     *
     * 屬性定義如下:
     *
     * - 該類具有與指定名稱相關聯的getter或setter方法(在本例中,屬性名不區分大小寫);
     * - 該類有一個具有指定名稱的成員變量(當' $checkVars '爲真時);
     *
     * @param string $name the property name
     * @param bool $checkVars whether to treat member variables as properties
     * @return bool whether the property is defined
     * @see canGetProperty()
     * @see canSetProperty()
     */
    public function hasProperty($name, $checkVars = true)
    {
        return $this->canGetProperty($name, $checkVars) || $this->canSetProperty($name, false);
    }

    /**
     * 返回一個值,該值指示是否可以讀取屬性
     *
     * 屬性是可讀的,如果:
     *
     * - 該類具有與指定名稱相關聯的getter方法(在本例中,屬性名不區分大小寫);
     * - 該類有一個具有指定名稱的成員變量(當' $checkVars '爲真時)
     *
     * @param string $name the property name
     * @param bool $checkVars whether to treat member variables as properties
     * @return bool whether the property can be read
     * @see canSetProperty()
     */
    public function canGetProperty($name, $checkVars = true)
    {
        return method_exists($this, 'get' . $name) || $checkVars && property_exists($this, $name);
    }

    /**
     * 返回一個值,該值指示是否可以設置屬性
     *
     * A property is writable if:
     *
     * - the class has a 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);
     *
     * @param string $name the property name
     * @param bool $checkVars whether to treat member variables as properties
     * @return bool whether the property can be written
     * @see canGetProperty()
     */
    public function canSetProperty($name, $checkVars = true)
    {
        return method_exists($this, 'set' . $name) || $checkVars && property_exists($this, $name);
    }

    /**
     * 返回一個值,該值指示是否定義了一個方法。
     *
     * The default implementation is a call to php function `method_exists()`.
     * You may override this method when you implemented the php magic method `__call()`.
     * @param string $name the method name
     * @return bool whether the method is defined
     */
    public function hasMethod($name)
    {
        return method_exists($this, $name);
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章