search
Homephp教程php手册ArrayObject扩展的应用示例代码

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);
    }
 
}



Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool