Home > php教程 > php手册 > body text

ArrayObject扩展的应用示例代码

WBOY
Release: 2016-06-21 08:58:07
Original
1166 people have browsed it
Zend_Config很好用,我比较喜欢它的数组形态,其实ArrayObject也可以做同样的事情

$config = array(
    'db' => 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所有的可用方法

 
/**
 * 将数组转换为对像形态使用
 *
 * @package    core
 * @author     Akon(番茄红了)
 * @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);
    }
 
}



Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!