Search code examples
phpapiquickbooks

Accessing nested objects within objects in PHP not working


In PHP, I'm requesting data from my QuickBooks Online account using their Query functions.

I'm iterating through each customer object that is returned in a standard foreach($customer as $x) code block.

Using var_dump, I can tell each object has the following format:

object(QuickBooksOnline\API\Data\IPPCustomer) [34]
public 'Taxable' => string 'true' (length=4)
public 'BillAddr' =>
    object(QuickBooksOnline\API\Data\IPPCustomer) [78]
    public 'Id' => string '7509' (length=4)
    public 'Line1' => '1234 Irrelevant Dr.' (length=19)

In PHP, if I simply try to access $x->Taxable, it works no problem, but any sub-object I try to access returns an error: "trying to get property of non-object on line X"

So:

echo $x->Taxable; 

gives me the taxable status, but

echo $x->BillAddr->Line1; 

gives me an error.

These are public classes, so shouldn't I be able to call them like this?


Solution

  • Found the problem and I have no idea why this is required, but here's how I got it to work, adding curly braces:

    echo "{$x->BillAddr->Line1}"; instead of just echo "$x->BillAddr->Line1";

    Same rule applies to assignment as well, apparently:

    $line1 = "{$x->BillAddr->Line1}";

    Thanks for your help, guys!