2
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.
Gerald Schneider
  • 17,416
  • 9
  • 60
  • 78
xyxy
  • 187
  • 1
  • 3
  • 9

3 Answers3

4

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)

Alma Do
  • 37,009
  • 9
  • 76
  • 105
  • Are you talking about getters and setters methods? http://stackoverflow.com/questions/1568091/why-use-getters-and-setters?rq=1 – pablofiumara Oct 01 '13 at 06:20
  • No. Not exactly. OP's class could have it's own methods to work with data - and I can not predict this `Term` class structure – Alma Do Oct 01 '13 at 06:21
0

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

DKSan
  • 4,187
  • 3
  • 25
  • 35
0

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.

Gerald Schneider
  • 17,416
  • 9
  • 60
  • 78