2

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

3
  • Which line number? Can you bold it or something? Commented Dec 12, 2014 at 20:36
  • on this code <td class="numeric"><?php echo $Type->getCardTypeId($cardType['card_type_id']); ?></td> <td class="numeric"><?php echo $Type->getCategory($cardType['category']); ?></td> Commented Dec 12, 2014 at 20:38
  • Basically changing $cardType['card_type_id'] into $cardType->card_type_id should do the trick. If you want to debug then just use var_dump($cardType); die(); before the error line. Commented Dec 12, 2014 at 20:41

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

Comments

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()

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.