Php 无法将类型为的对象XXX用作数组

Php 无法将类型为的对象XXX用作数组,php,arrays,class,phpunit,Php,Arrays,Class,Phpunit,我正在运行抛出错误的函数: 无法使用类型为数组的对象联盟/配置存储库 为了解决这个问题,我需要在扩展类“ConfigRepository”中进行更改 <?php use Coalition\ConfigRepository; class ConfigRepositoryTest extends PHPUnit_Framework_TestCase public function test_array_access_set() { $config = new Co

我正在运行抛出错误的函数:

无法使用类型为数组的对象联盟/配置存储库

为了解决这个问题,我需要在扩展类“ConfigRepository”中进行更改

<?php

use Coalition\ConfigRepository;

class ConfigRepositoryTest extends PHPUnit_Framework_TestCase
public function test_array_access_set()
    {
        $config = new ConfigRepository;

        $config['foo'] = 'bar'; //throw error here
        $this->assertTrue(isset($config['foo']));
        $this->assertEquals('bar', $config['foo']);
    }
}
public function test_array_access_unset()
    {
        $config = new ConfigRepository(['foo' => 'bar']);
        unset($config['foo']);

        $this->assertFalse($config->has('foo'));
    }
我怎样才能解决它


可能问题出在
\u构造中
我必须传递数组值的地方?

最有效的修复方法是将
$key
成员公开。所以第一个变化是在
类ConfigRepository
中:

public $key=[];
然后你可以做:

public function test_array_access_set() {
    $config = new ConfigRepository(array("foo" => "bar")); // set the value in the constructor 

    // access the $config->key as you array and check what you need
    $this->assertTrue(isset($config->key['foo'])); 
    $this->assertEquals('bar', $config->key['foo']);
}
如果您只能更改ConfigRepository Cals,则应执行以下操作:

class ConfigRepository implements ArrayAccess {

    private $container = array();

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

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

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

    public function offsetSet($offset, $value) {
        if (is_null($offset)) 
            $this->container[] = $value;
        else
            $this->container[$offset] = $value;
    }

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

}

错误已清除-对象不是数组。你想达到什么目标?是否希望ConfigRepository保存关联数组?如果
$key
是私有的,你至少应该有
get
方法(如果没有也设置/附加)我想解决这个错误,因为我必须在下面的类中更改,并且值应该是数组,请通过更改类配置repository更新答案我不能在test\u array\u access\u set()中更改改为函数我只需更改ConfigRepository类您是否愿意更改类而不是对象??真奇怪。。。在这种情况下,请看:关于如何实现类似数组的对象(这里:)是的,我已经通过了所有这些方法,只是这一个不是working@KetanBorada这是完整示例的链接:。我不知道我还能帮什么忙——也许你的问题在别处。ArrayAccess是成功的关键
class ConfigRepository implements ArrayAccess {

    private $container = array();

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

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

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

    public function offsetSet($offset, $value) {
        if (is_null($offset)) 
            $this->container[] = $value;
        else
            $this->container[$offset] = $value;
    }

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

}