0

Im returning an array full of 3 objects to a function where i simply want to access the 3rd tier data.

Now when returned, i can var_dump the whole array and i can see it, i can also var_dump the array plus the number of the object i would like to use and see it. An example below:

I use this code:

$data = Container::make_orderList();
    var_dump($data);

And i get this result:

array (size=3)
0 => 
object(order)[63]
  private 'increment_id' => string '100000002' (length=9)
  private 'created_date' => string '2013-10-19 19:34:02' (length=19)
  private 'is_active' => null
  private 'weight' => string '20.0000' (length=7)
  private 'status' => string 'processing' (length=10)
1 => 
object(address)[55]
  private 'company_name' => string 'No Company' (length=10)
  private 'street' => string '20 Waterfall Way 
  Barwell' (length=25)
  private 'city' => string 'Leicester' (length=9)
  private 'region' => string 'Leicestershire' (length=14)
  private 'postcode' => string 'LE9 8EH' (length=7)
2 => 
object(address)[54]
  private 'company_name' => string 'CRanbri Web Solutions' (length=21)
  private 'street' => string '4 Turner Drive
  Hinckley' (length=23)
  private 'city' => string 'Leciester' (length=9)
  private 'region' => string 'LEicesterhsire' (length=14)
  private 'postcode' => string 'LE10 0gu' (length=8)
  private 'country_id' => string 'GB' (length=2)

and with:

$data = Container::make_orderList();
    var_dump($data[0]);

I get this result:

object(order)[63]
 private 'increment_id' => string '100000002' (length=9)
 private 'created_date' => string '2013-10-19 19:34:02' (length=19)
 private 'is_active' => null
 private 'weight' => string '20.0000' (length=7)
 private 'status' => string 'processing' (length=10)
 private 'shipping_address_id' => string '4' (length=1)
 private 'billing_address_id' => string '3' (length=1)
 private 'shipping_method' => string 'flatrate_flatrate' (length=17)
 private 'shipping_description' => string 'Flat Rate - Fixed' (length=17)
 private 'order_id' => string '2' (length=1)
 private 'gift_message' => null

This is great and what i need, but what im having trouble doing is accessing the individual fields of the array say for example the 'increment_id' field.

I have tried:

$data = Container::make_orderList();
    var_dump($data[0]->increment_id);

But i get this error:

Fatal error: Cannot access private property order::$increment_id in C:\xampp\htdocs\magento_soap_client\fulfilment\soap\views\view.content.php on line 20

I don't suppose you could help me put with how i should access these fields?

Thanks

4
  • as error says, your fields are private so you should make getters/setters or make them public Commented Oct 25, 2013 at 9:49
  • the php syntax does not accept this format, you should first assign $data[0] to a varible like $obj, then access its fields:$obj->increment_id also your fields are private and are not visible outside of the class Commented Oct 25, 2013 at 9:49
  • Private means that you cant access them from outside. Thats why its private. You have to make them public. Commented Oct 25, 2013 at 9:53
  • Yeah thanks guys, all set to public now! blonde moment i think! Commented Oct 25, 2013 at 10:00

1 Answer 1

2

Just make the fields of your object public or create getters and/or setters for the fields. You might want to take a look at the documentation about visibility of properties and methods in PHP.

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

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.