zend_config很好用,我比较喜欢它的数组形态,其实arrayobject也可以做同样的事情
array (
'adapter' => 'mysql',
'config' => array (
'host' => 'localhost',
'port' => '3306',
'dbname' => 'mydbname',
'username' => 'dbuser',
'password' => 'dbpassword',
'charset' => 'utf8',
'prefix' => '',
),
),
);
$config = new zend_config($config);
echo $config->db->adapter;
foreach ($config->db->config as $k => $v) {
echo $k | $v \n;
}
echo count($config);
//... 甚至其他更多的方法
下面的扩展,通过几个魔术方法,不仅可以实现zend_config可以做到的事情,还可以继承array_object所有的可用方法
offsetget($index);
}
/**
* 使用魔术方法修改指定 name 的值
*
* @param string $index
* @param mixed $value
*/
public function __set($index, $value)
{
$this->offsetset($index, $value);
}
/**
* 通过魔术方法判断数据是否已被设置
*
* @param string $index
* @return boolean
*/
public function __isset($index)
{
return $this->offsetexists($index);
}
/**
* 通过魔术方法删除数据
*
* @param string $index
*/
public function __unset($index)
{
$this->offsetunset($index);
}
/**
* 将数据信息转换为数组形式
*
* @return array
*/
public function toarray()
{
$array = $this->getarraycopy();
foreach ($array as &$value)
($value instanceof self) && $value = $value->toarray();
return $array;
}
/**
* 将数据组转换为字符串形式
*
* @return array
*/
public function __tostring()
{
return var_export($this->toarray(), true);
}
}