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
<td class="numeric"><?php echo $Type->getCardTypeId($cardType['card_type_id']); ?></td> <td class="numeric"><?php echo $Type->getCategory($cardType['category']); ?></td>$cardType['card_type_id']into$cardType->card_type_idshould do the trick. If you want to debug then just usevar_dump($cardType); die();before the error line.