Application sample code for Arrayobject extension

Source: Internet
Author: User
Zend_config is very easy to use, I prefer its array form, in fact, Arrayobject can do the same thing
<?php
$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);
//... And even more ways

The following extensions, through a few magic methods, can not only achieve what zend_config can do, but also inherit array_object all available methods

<?php

/**
* Converts an array to a pair-like shape using
*
* @package Core
* @author Akon (tomato red) <aultoale@gmail.com>
* @copyright Copyright (c) 2008 (http://www.tblog.com.cn)
* @license http://www.gnu.org/licenses/gpl.html GPL 2
*/
Class Extend_arrayobject extends Arrayobject
{

/**
* Construction Method
*
* @param array $array
*/
Public function __construct (array $array = Array ())
{
foreach ($array as & $value)
Is_array ($value) && $value = new self ($value);
Parent::__construct ($array);
}

/**
* Get value by specifying name using magic method
*
* @param string $index
* @return Mixed
*/
Public Function __get ($index)
{
return $this->offsetget ($index);
}

/**
* Use magic method to modify the value of the specified name
*
* @param string $index
* @param mixed $value
*/
Public Function __set ($index, $value)
{
$this->offsetset ($index, $value);
}

/**
* By magic method to determine whether the data has been set
*
* @param string $index
* @return Boolean
*/
Public Function __isset ($index)
{
return $this->offsetexists ($index);
}

/**
* Remove data by magic method
*
* @param string $index
*/
Public Function __unset ($index)
{
$this->offsetunset ($index);
}

/**
* Transform data information into arrays
*
* @return Array
*/
Public Function ToArray ()
{
$array = $this->getarraycopy ();
foreach ($array as & $value)
($value instanceof self) && $value = $value->toarray ();
return $array;
}

/**
* Convert data Group to string form
*
* @return Array
*/
Public Function __tostring ()
{
Return Var_export ($this->toarray (), true);
}

}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.