【问题标题】:Fatal Error : Cannot use object of type as array致命错误:不能使用类型的对象作为数组
【发布时间】:2014-12-12 20:35:10
【问题描述】:

所以,当我尝试从对象单例中获取数组列表时出现了这个错误。

这个是 CardType.php 上的单例

class CardTypeAPIAccessor extends GuzzleClient
{

    private $client;

    public function __construct($client) 
    {
        if($client instanceof GuzzleClient)
        {
            $this->client = $client;
        }
        else
        {
            $this->client = parent::getClient();
        }
    }

    public function getCardTypes() {
        $cardTypes = array();

        try 
        {

            $response = $this->client->get('admin/card/type',
                ['headers' => ['Authorization' => $_SESSION['login']['apiKey']]
            ]);

            $statusCode = $response->getStatusCode();
            // Check that the request is successful.
            if ($statusCode == 200) 
            {
                $error = $response->json();
                foreach ($error['types'] as $type) 
                {
                    $cardType = new CardType();
                    $cardType->setCardTypeId($type['card_type_id']);
                    $cardType->setCategory($type['category']);

                    array_push($cardTypes, $cardType);
                }
            }
        }
        catch(RequestException $e) 
        {
            //todo
            echo $e->getRequest() . "</br>";
            if ($e->hasResponse()) 
            {
                echo $e->getResponse() . "</br>";
            }
        }
        return $cardTypes;      
    }
}

这是 card_type.php 上的代码

<?php
     $cardTypes = $cardTypeAPIAccessor->getCardTypes();
     foreach ($cardTypes as $cardType){
     $Type = new CardType();
?>
     <tr>
        <!-- The error is here-->
        <td class="numeric"><?php echo $Type->getCardTypeId($cardType['card_type_id']); ?></td>
        <td class="numeric"><?php echo $Type->getCategory($cardType['category']); ?></td>
     </tr>

错误提示:不能使用 CardType 类型的对象作为数组

我的代码出错了吗?

谢谢

【问题讨论】:

  • 第几行?你能加粗吗?
  • 在此代码上&lt;td class="numeric"&gt;&lt;?php echo $Type-&gt;getCardTypeId($cardType['card_type_id']); ?&gt;&lt;/td&gt; &lt;td class="numeric"&gt;&lt;?php echo $Type-&gt;getCategory($cardType['category']); ?&gt;&lt;/td&gt;
  • 基本上将$cardType['card_type_id'] 更改为$cardType-&gt;card_type_id 应该可以解决问题。如果要调试,只需在错误行之前使用var_dump($cardType); die();

标签: php arrays object


【解决方案1】:

看起来您已经获得了这一行所需的所有值:

$cardTypes = $cardTypeAPIAccessor->getCardTypes();

所以在我看来,在你的循环中,你只需要:

foreach ($cardTypes as $cardType){
?>
 <tr>
    <td class="numeric"><?php echo $cardType->getCardTypeId(); ?></td>
    <td class="numeric"><?php echo $cardType->getCategory(); ?></td>
 </tr>

虽然这实际上只是基于给定代码和缺少的CardType 类的猜测。但我认为不需要在循环内生成新对象。

【讨论】:

    【解决方案2】:

    $cardType 变量是 CardType 类的实例,而不是数组。问题是当它是一个对象时,你正在使用它,就好像它是一个数组一样。所以这个

    $cardType['card_type_id']
    

    应该是

    $cardType->getCardTypeID()
    

    【讨论】:

      【解决方案3】:

      有时您的响应会变得基于对象,无法轻松访问。用例,例如当您从控制器或任何第三方 API 中的不同控制器获取数据时。

      所以你可以使用这种方式获取数据

      $object->getData(1);   // here 1 is for associated data form.
      

      在我的用例中,当我尝试从不同的控制器访问数据时,我遇到了同样的问题。

          $attendanceController =  new AttendanceController();
      
          $offDayData =  $attendanceController->getAttendanceOffDays($req);
      
          return  $offDayData->getData(1)['response'];
      

      当您尝试从 Controller 类 获取数据时,响应会以不同的方式包装。因此您必须使用 getData(1) 方法来访问该响应。在我的解决方案中,我使用了它。它适用于所有情况,然后你想从不同的地方获取数据 ControllerCalss

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-19
        • 2013-07-05
        • 2019-06-19
        • 1970-01-01
        相关资源
        最近更新 更多