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

问题描述

我有一个类实现 ArrayAccess接口,我试图让它与多维数组工作。 存在 GET 的工作。 设置取消设置虽然是给了我一个问题。

I have a class implementing ArrayAccess and I'm trying to get it to work with a multidimensional array. exists and get work. set and unset are giving me a problem though.

class ArrayTest implements ArrayAccess {
    private $_arr = array(
        'test' => array(
            'bar' => 1,
            'baz' => 2
        )
    );

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

    public function offsetSet($name, $value) {
        $this->_arr[$name] = $value;
    }

    public function offsetGet($name) {
        return $this->_arr[$name];
    }

    public function offsetUnset($name) {
        unset($this->_arr[$name]);
    }
}

$arrTest = new ArrayTest();


isset($arrTest['test']['bar']);  // Returns TRUE

echo $arrTest['test']['baz'];    // Echo's 2

unset($arrTest['test']['bar'];   // Error
$arrTest['test']['bar'] = 5;     // Error

我知道 $ _改编可能只是被公开,所以你可以直接访问它,但我实现它不想要的,是私有的。

I know $_arr could just be made public so you could access it directly, but for my implementation it's not desired and is private.

最后2行抛出一个错误:注意:重载元素的间接修改

The last 2 lines throw an error: Notice: Indirect modification of overloaded element.

我知道 ArrayAccess接口只是一般不与多维数组工作,但反正是有解决这个或任何稍微干净的实施,将允许所需的功能?

I know ArrayAccess just generally doesn't work with multidimensional arrays, but is there anyway around this or any somewhat clean implementation that will allow the desired functionality?

我能想出是用字符作为分隔符,并在测试为它设置取消设置并采取相应的行动。如果你正在处理一个可变深度虽然这变得非常丑陋的真快。

The best idea I could come up with is using a character as a separator and testing for it in set and unset and acting accordingly. Though this gets really ugly really fast if you're dealing with a variable depth.

有谁知道为什么存在 GET 工作,以便在功能可能复制?

Does anyone know why exists and get work so as to maybe copy over the functionality?

感谢任何帮助任何人都可以提供。

Thanks for any help anyone can offer.

推荐答案

问题的可以改变解决公共职能offsetGet($名)公共职能和放大器; (加入引用返回),<强offsetGet($名)>,但会引起致命错误( ArrayTest中:: offsetGet()的声明必须与了ArrayAccess :: offsetGet()的)。

The problem could be resolved by changing public function offsetGet($name) to public function &offsetGet($name) (by adding return by reference), but it will cause Fatal Error ("Declaration of ArrayTest::offsetGet() must be compatible with that of ArrayAccess::offsetGet()").

PHP作者搞砸了与此类前一段时间,现在他们:

PHP authors screwed up with this class some time ago and now they won't change it in sake of backwards compatibility:

我们发现,这是不可解
  没有炸毁的接口和
  创建BC或提供
  更多接口的支持
  引用和由此产生
  内部恶梦 - 其实我也不
  看到一个方法,我们可以永远做这项工作。
  因此,我们决定将强制执行
  原创设计,并禁止
  引用completley。

编辑:如果您仍然需要这种功能,我建议使用魔术方法来代替( __得到() __集()等),因为 __的ge​​t()引用返回值。这将改变语法是这样的:

If you still need that functionality, I'd suggest using magic method instead (__get(), __set(), etc.), because __get() returns value by reference. This will change syntax to something like this:

$arrTest->test['bar'] = 5;

当然不是一个理想的解决方案,但我想不出一个更好的。

Not an ideal solution of course, but I can't think of a better one.

更新:此问题并现在ArrayAccess接口按预期工作:

Update: This problem was fixed in PHP 5.3.4 and ArrayAccess now works as expected:

在PHP 5.3.4开始,原型检查都是轻松和有可能为这种方法通过引用返回的实现。这使得间接修改了ArrayAccess对象可能重载阵列尺寸。

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

09-24 19:53