Zend_Config很好用,我比较喜欢它的数组形态,其实ArrayObject也可以做同样的事情 <?php $config = array( ‘db’ => array ( ‘adapter’ => ‘mysql‘, ‘config’ => array ( http://www.gaodaima.com/48287.htmlArrayObject扩展的应用示例代码_php ‘host’ => ‘localhost’, 下面的扩展,通过几个魔术方法,不仅可以实现Zend_Config可以做到的事情,还可以继承Array_Object所有的可用方法 <?php /** * 将数组转换为对像形态使用 * * @package core * @author Akon(番茄红了) <[email protected]> * @copyright Copyright (c) 2008 (http://www.tblog.com.cn) * @license http://www.gnu.org/licenses/gpl.html GPL 2 */ class Extend_ArrayObject extends ArrayObject { /** * 构造方法 * * @param array $array */ public function __construct(array $array = array()) { foreach ($array as &$value) is_array($value) && $value = new self($value); parent::__construct($array); } /** * 使用魔术方法通过指定 name 获取值 * * @param string $index * @return mixed */ public function __get($index) { return $this->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); } } |
欢迎大家阅读《ArrayObject扩展的应用示例代码_php》,跪求各位点评,若觉得好的话请收藏本文,by 搞代码