簡體   English   中英

致命錯誤:無法將類型的對象用作數組

[英]Fatal Error : Cannot use object of type as array

因此,當我嘗試從對象單例中獲取數組列表時,出現了此錯誤。

這是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類型的對象用作數組

我的代碼出錯了嗎?

謝謝

看來您已經獲得了以下行的所有必要值:

$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類的猜測。 但我認為不需要在循環內生成新對象。

$cardType變量是CardType類的實例,而不是數組。 問題是,當它是一個對象時,您就像在使用數組一樣使用它。 所以這

$cardType['card_type_id']

應該

$cardType->getCardTypeID()

有時您的響應會變成基於對象的,無法輕易訪問。 用例,例如從控制器或任何第三方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獲取數據

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM