Fatal Error : Cannot use object of type as array

13,318

Solution 1

It looks like you have already gotten all values necessary with this line:

$cardTypes = $cardTypeAPIAccessor->getCardTypes();

So it would seem to me that in your loop, you just need:

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

Although that is really just a guess based on the given code and the missing CardType class. But I don't see the need to generate new objects inside the loop.

Solution 2

The $cardType variable is an instance of the CardType class, not an array. The problem is you are using it as though it were an array, when it is an object. So this

$cardType['card_type_id']

Should be

$cardType->getCardTypeID()

Solution 3

sometimes your response becomes object-based that can not easily access. use case like when you get data from a different controller in a controller or any third-party API.

so you can get data using this way

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

In my use case, I was facing the same problem when I try to access data from a different controller.

    $attendanceController =  new AttendanceController();

    $offDayData =  $attendanceController->getAttendanceOffDays($req);

    return  $offDayData->getData(1)['response'];

When you try to get data from a Controller Class then response wrapped in a different way. so that you have to use getData(1) method for access that response. in my solution i used that. it will work in all case then you want to get data from different ControllerCalss

Share:
13,318
Monk
Author by

Monk

Updated on June 04, 2022

Comments

  • Monk
    Monk almost 2 years

    So, I this error appeared when I try to get a list of array from object singleton.

    This one is the singleton on 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;      
        }
    }
    

    And this is the code on 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>
    

    The error said :Cannot use object of type CardType as array

    Is my code went wrong ?

    Thanks