不能使用stdClass类型的对象作为数组

时间:2012-03-01 16:38:07

标签: php amfphp

首先,我不是一个Php极客..请原谅我对PHP内容的一点知识。 我在前端使用AMFPHP2和Flex。 我可以从后端获取数据作为打字对象很好,但是当我尝试保存时,我遇到如下问题:

<br /><b>Fatal error</b>:  Cannot use object of type stdClass as array in <b>/mnt/array1/share/htdocs/htdocs/admin/application/amf/services/Item.php</b> on line <b>88</b><br />

以下是抛出此错误的代码:

Line86    public function saveCollection($collection) {
Line87        for ($i=0; $i<count($collection); $i++) {
Line88            $this->saveItem($collection[$i]);
Line89        }
Line90    }

以下是我的VO课程: ItemVO.php

class ItemVO {
..
..
var $_explicitType = "ItemVO";
..
..
}

ItemVO.as

package models.vo {
    [RemoteClass(alias="ItemVO")]  
    public class ItemVO {
...
...
    }
}

这是我的文件夹结构:

-root/
------*.html
------*.swf
------application/
-----------------amf/
--------------------/index.php
--------------------/models/vo/all vo files
--------------------/services/all services
-----------------libraries/
--------------------------/Amfphp/

这是我的index.php

<?php
require_once dirname(dirname(__FILE__)) . '/libraries/Amfphp/ClassLoader.php';
$config = new Amfphp_Core_Config();
$config->serviceFolderPaths = array(dirname(__FILE__) . '/services/');
$voFolders = array(dirname(__FILE__) . '/models/vo/');
$customClassConverterConfig = array(‘customClassFolderPaths’ => $voFolders);
$config->pluginsConfig['AmfphpCustomClassConverter'] = $customClassConverterConfig;
$gateway = Amfphp_Core_HttpRequestGatewayFactory::createGateway($config);
$gateway->service();
$gateway->output();
?>

任何帮助将不胜感激。 感谢。

3 个答案:

答案 0 :(得分:3)

我对错误的基本理解是你试图访问一个对象,好像它是一个数组。

这通常意味着您正在执行$something['something']而不是正确的$something->something

$collection中的saveCollection参数是数组还是对象?

尝试用以下代码替换第88行:

 $this->saveItem($collection->$i);

修改

正如我刚刚在评论中意识到的那样,它无论如何都不应该起作用,因为你试图计算stdClass。正如其他人在答案中提到的那样,使用for each应该可以做到这一点。

答案 1 :(得分:3)

错误信息是自我解释的。 您可以使用例如这样:

public function saveCollection($collection) {
 foreach ($collection as $value) {
          $this->saveItem($value);
       }
   }

答案 2 :(得分:0)

您正在引用一个stdClass 对象,就像数组一样,两者不是一回事。为了您的目的,您可以将其转换为数组:

public function saveCollection($collection) {
    $collection = (array)$collection;
    for ($i=0; $i<count($collection); $i++) {
        $this->saveItem($collection[$i]);
    }
}

注意:将对象作为数组进行转换并不总是有效,但由于看起来你希望传入一个类似数组的结构,它可能会正常工作。

相关问题