码迷,mamicode.com
首页 > 其他好文 > 详细

yii2框架随笔19

时间:2016-05-01 21:49:40      阅读:335      评论:0      收藏:0      [点我收藏+]

标签:

    public function canGetProperty($name, $checkVars = true)
    {
        // property_exists — 检查对象或类是否具有该属性
        return method_exists($this, get . $name) || $checkVars && property_exists($this, $name);
    }
    /**
     * Returns a value indicating whether a property can be set.
     * 返回一个值指示是否可以设置一个属性。
     * 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);
     *
     * 检查对象或类是否能够设置 $name 属性,如果 $checkVars 为 true,则不局限于是否有 setter
     *
     * @param string $name the property name
     * @param boolean $checkVars whether to treat member variables as properties
     * @return boolean 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);//检查对象或类是否能够设置 $name 属性,返回Boolean值。
    }
    /**
     * Returns a value indicating whether a method is defined.
     * 返回一个值指示是否定义了一个方法。
     * The default implementation is a call to php function `method_exists()`.
     * You may override this method when you implemented the php magic method `__call()`.
     *
     * 检查对象或类是否具有 $name 方法
     *
     * @param string $name the method name
     * @return boolean whether the method is defined
     */
    public function hasMethod($name)
    {
        return method_exists($this, $name);//返回Boolean值。
    }
    
}

下面来介绍console/Controller.php

<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */
namespace yii\console;
use Yii;
use yii\base\Action;
use yii\base\InlineAction;
use yii\base\InvalidRouteException;
use yii\helpers\Console;
/**
 * Controller is the base class of console command classes.
 * 控制器是控制台命令类的基类.
 *
 * A console controller consists of one or several actions known as sub-commands.
 * 一个控制台控制器由一个或几个动作称为sub-commands。
 * Users call a console command by specifying the corresponding route which identifies a controller action.
 * 用户调用一个控制台命令通过指定相应的路线确定控制器动作
 * The `yii` program is used when calling a console command, like the following:
 * yii的程序调用时使用控制台命令,如下:
 *
 * ~~~
 * yii <route> [--param1=value1 --param2 ...]
 * ~~~
 *
 *
 * @property string $help This property is read-only.
 * @property string $helpSummary This property is read-only.
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
class Controller extends \yii\base\Controller
{
    const EXIT_CODE_NORMAL = 0;
    const EXIT_CODE_ERROR = 1;
    /**
     * @var boolean whether to run the command interactively.
     * 是否运行该命令交互
     */
    public $interactive = true;
    /**
     * @var boolean whether to enable ANSI color in the output.
     * 是否支持ANSI颜色在输出
     * If not set, ANSI color will only be enabled for terminals that support it.
     * 如果没有设置,ANSI颜色只会支持终端支持它。
     */
    public $color;
  /**
     * Runs an action with the specified action ID and parameters.
     * 运行一个动作与ID指定的操作和参数。
     * If the action ID is empty, the method will use [[defaultAction]].
     * 如果行动ID为空,该方法将使用[[defaultAction]]。
     * @param string $id the ID of the action to be executed.
     * @param array $params the parameters (name-value pairs) to be passed to the action.
     * @return integer the status of the action execution. 0 means normal, other values mean abnormal.
     * @throws InvalidRouteException if the requested action ID cannot be resolved into an action successfully.
     * @throws Exception if there are unknown options or missing arguments
     * @see createAction
     */
    public function runAction($id, $params = [])
    {
        if (!empty($params)) {
            // populate options here so that they are available in beforeAction().
            // 获取允许的参数,可以在beforeAction中拿到
            $options = $this->options($id);
            foreach ($params as $name => $value) {
                if (in_array($name, $options, true)) {
                    // 如果参数在允许的options,才会将值赋给controller的属性
                    $default = $this->$name;
                    // 如果该属性的默认值是数组,就根据逗号和空格(包含" ", \r, \t, \n, \f)的组合分隔value值
                    $this->$name = is_array($default) ? preg_split(/\s*,\s*/, $value) : $value;
                    unset($params[$name]);
                } elseif (!is_int($name)) {
                    throw new Exception(Yii::t(yii, Unknown option: --{name}, [name => $name]));
                }
            }
        }
        return parent::runAction($id, $params);
    }

 

yii2框架随笔19

标签:

原文地址:http://www.cnblogs.com/taokai/p/5451188.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!