Yii Object的配置方法

来源:互联网 发布:西门子300plc编程实例 编辑:程序博客网 时间:2024/06/04 01:25
以下内容文字来源于请猛戳这里,对于学习Yii本人认为十分有帮助,代码来自于Yii源码!!!!
$config = yii\helpers\ArrayHelper::merge(
    require(__DIR__ . '/../../common/config/main.php'),
    require(__DIR__ . '/../../common/config/main-local.php'),
    require(__DIR__ . '/../config/main.php'),
    require(__DIR__ . '/../config/main-local.php')
);

$application = new yii\web\Application($config);

public static function merge($a, $b)
{
$args = func_get_args();
$res = array_shift($args);
while (!empty($args)) {
$next = array_shift($args);
foreach ($next as $k => $v) {
if (is_int($k)) {
if (isset($res[$k])) {
$res[] = $v;
} else {
$res[$k] = $v;
}
} elseif (is_array($v) && isset($res[$k]) && is_array($res[$k])) {
$res[$k] = self::merge($res[$k], $v);
} else {
$res[$k] = $v;
}
}
}

return $res;
}

public function __construct($config = [])
{

//empty()函数是用来测试变量是否已经配置。若变量已存在、非空字符串或者非零,则返回 false 值;反之返回 true值
if (!empty($config)) {
Yii::configure($this, $config);
}
$this->init();
}

public static function configure($object, $properties)
{
foreach ($properties as $name => $value) {
$object->$name = $value;//这里可以看出对象属性要有setter方法
}

return $object;
}

同时请看这里组件编码风格
0 0
原创粉丝点击