PHP ArrayAccess接口设置多维 [英] PHP ArrayAccess set multidimensional

查看:266
本文介绍了PHP ArrayAccess接口设置多维的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:我意识到文字的数量可能会被恐吓。这个问题的实质:结果
如何的方式,使设置多维价值可能实现了ArrayAccess?

I realized the amount of text might be intimidating. The essence of this question:
How to implement ArrayAccess in a way that makes setting multidimensional values possible?

 

 

据我所知,这是讨论这里已经但是我似乎无法正确实现了ArrayAccess接口。

I am aware that this was discussed here already but I seem unable to implement the ArrayAccess interface correctly.

基本上,我有一个类的数组来处理应用程序的配置和实施 ArrayAccess接口。检索值从嵌套的按键做工精细,甚至值( $端口= $配置['应用'] ['端口']; )。设定值仅适用于一维数组,虽然:当我尝试(UN)设置一个值(如在previous例如端口),我得到了以下错误消息:

Basically, I've got a class to handle the app configuration with an array and implemented ArrayAccess. Retrieving values works fine, even values from nested keys ($port = $config['app']['port'];). Setting values works only for one-dimensional arrays, though: As soon as I try to (un)set a value (eg. the port in the previous example), i get the following error message:

Notice:  Indirect modification of overloaded element <object name> has no effect in <file> on <line>

现在普遍的看法似乎是,在 offsetGet()方法参照返回(&放大器; offsetGet())。然而,这并没有解决问题,恐怕我不知道如何正确地实施方法 - 为什么使用getter方法​​来设置一个值?这里的PHP文档 是不是真的有帮助的任何。

Now the general opinion seems to be that the offsetGet() method has to return by reference (&offsetGet()). That, however, does not solve the problem and I'm afraid I don't know how to implement that method correctly - why is a getter method used to set a value? The php doc here is not really helpful either.

要直接复制此(PHP 5.4-5.6),请在下面找到连接样本code:

To directly replicate this (PHP 5.4-5.6), please find a sample code attached below:

<?php

class Config implements \ArrayAccess
{

    private $data = array();

    public function __construct($data)
    {
        $this->data = $data;
    }


    /**
     * ArrayAccess Interface
     * 
     */
    public function offsetSet($offset, $value)
    {
        if (is_null($offset)) {
            $this->data[] = $value;
        } else {
            $this->data[$offset] = $value;
        }
    }

    public function &offsetGet($offset)
    {       
        return isset($this->data[$offset]) ? $this->data[$offset] : null;
    }

    public function offsetExists($offset)
    {
        return isset($this->data[$offset]);
    }

    public function offsetUnset($offset)
    {
        unset($this->data[$offset]);
    }
}

$conf = new Config(array('a' => 'foo', 'b' => 'bar', 'c' => array('sub' => 'baz')));
$conf['c']['sub'] = 'notbaz';

&NBSP;

&NBSP;

编辑2:的解决方案,瑞恩指出的那样,是使用ArrayObject的,而不是(这已经实现了 ArrayAccess接口可数 IteratorAggregate )。结果
将它应用到一类抱着一个数组,结构,它像这样:

EDIT 2: The solution, as Ryan pointed out, was to use ArrayObject instead (which already implements ArrayAccess, Countable and IteratorAggregate).
To apply it to a class holding an array, structure it like so:

<?php

class Config extends \ArrayObject
{

    private $data = array();

    public function __construct($data)
    {
        $this->data = $data;
        parent::__construct($this->data);
    }


    /**
     * Iterator Interface
     *
     */
    public function getIterator() {
        return new \ArrayIterator($this->data);
    }

    /**
     * Count Interface
     *
     */
    public function count()
    {
        return count($this->data);
    }
}

&NBSP;

我用这对我的配置库 libconfig 这是可以上的 Github上MIT许可下的。

I used this for my Config library libconfig which is available on Github under the MIT license.

推荐答案

我不知道这是否会是有用的。我已经注意到的 ArrayObject的 的类是有趣......

I am not sure if this will be useful. I have noticed that the ArrayObject class is 'interesting'...

我不知道,这是一个连答案。它更多的是这个类的观察。

I am not sure that this is even an 'answer'. It is more an observation about this class.

它能够正确处理了多维数组的东西作为标准配置。

It handles the 'multidimensional array' stuff correctly as standard.

您可能能够添加方法,使其做更多你想什么呢?

You may be able to add methods to make it do more of what you wish?

<?php //

class Config extends \ArrayObject
{

//    private $data = array();

    public function __construct(array $data = array())
    {
        parent::__construct($data);
    }
}

$conf = new Config(array('a' => 'foo', 'b' => 'bar', 'c' => array('sub' => 'baz')));
$conf['c']['sub'] = 'notbaz';
$conf['c']['sub2'] = 'notbaz2';

var_dump($conf, $conf['c'], $conf['c']['sub']);

unset($conf['c']['sub']);

var_dump('isset?: ', isset($conf['c']['sub']));

var_dump($conf, $conf['c'], $conf['c']['sub2']);

输出:

object(Config)[1]
  public 'a' => string 'foo' (length=3)
  public 'b' => string 'bar' (length=3)
  public 'c' => 
    array
      'sub' => string 'notbaz' (length=6)
      'sub2' => string 'notbaz2' (length=7)

array
  'sub' => string 'notbaz' (length=6)
  'sub2' => string 'notbaz2' (length=7)

string 'notbaz' (length=6)

string 'isset?: ' (length=8)

boolean false

object(Config)[1]
  public 'a' => string 'foo' (length=3)
  public 'b' => string 'bar' (length=3)
  public 'c' => 
    array
      'sub2' => string 'notbaz2' (length=7)

array
  'sub2' => string 'notbaz2' (length=7)

string 'notbaz2' (length=7)

这篇关于PHP ArrayAccess接口设置多维的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆