xyxy
xyxy

Reputation: 197

Access data from a var_dump()

object(Term)#32 (10) {
  ["term_id":protected]=> int(11589)
  ["session_id":protected]=> string(5) "11275"
  ["site_id":protected]=> int(9999999)
  ["data":protected]=> array(62) {
    ["term_id"]=> string(5) "11589"
    ["term_name"]=> string(9) "Full Year"
    ["start_date"]=> string(10) "2013-09-02"
    ["end_date"]=> string(10) "2014-06-14" 
  }
}

I get this data from a var_dump and I want to access "start_date". How to do this?

let's say

var_dump($term);

I did:

var_dump($term["start_date"]); and I get a NULL.

Upvotes: 2

Views: 1415

Answers (3)

Alma Do
Alma Do

Reputation: 37365

You should not do that. var_dump is a debugging function, so it's output is similar to internal representation of variable (not exact, of cause) - and it should not be used in any other cases rather than debugging.

Since your object data that you want to get is protected, you should use corresponding method to get/modify that (see your Term class definition)

Upvotes: 4

Gerald Schneider
Gerald Schneider

Reputation: 17797

Your object $term does not have an index start_date, it is not an array. Also, the property data is protected, so it can only be accessed from inside of the object.

If you remove the protected flag from the object it will be accessible like this:

var_dump($term->data["start_date"]);

This accesses the index start_date in the array data inside of the $term object.

An alternative would be to add a getter function for the value to the Term class.

Upvotes: 0

DKSan
DKSan

Reputation: 4197

You can not access the property start_date in that way. Your syntax would work if $term was an array, but not with an object.

The object needs a getter for the protected property start_date

Upvotes: 0

Related Questions