How to get the names of all subobjects?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • René

    How to get the names of all subobjects?

    Hello,

    My example:

    $person->telnumber->value
    $person->faxnumber->value
    $person->???

    The names of the objects "telnumber" and "faxnumber" (and further
    possible subobjects) are not known for me.

    How can I get the names of all subobjects of an object?

    Thanks a lot.
    René
  • Børge Alvestad

    #2
    Re: How to get the names of all subobjects?


    "René" <google@teinze. com> wrote in message
    news:56e53b7b.0 309071101.43ef6 c76@posting.goo gle.com...[color=blue]
    > Hello,
    >
    > My example:
    >
    > $person->telnumber->value
    > $person->faxnumber->value
    > $person->???
    >
    > The names of the objects "telnumber" and "faxnumber" (and further
    > possible subobjects) are not known for me.
    >
    > How can I get the names of all subobjects of an object?
    >
    > Thanks a lot.
    > René[/color]

    I reckon you're talking arrays here...(?)
    You can then find both the keys and values of the array by doing this;

    foreach ($person as $key => $value) {
    echo "$key is $value \n";
    }

    I.e. if (by your examples) some keys and values are like this[color=blue]
    > $person->telnumber->555-1515
    > $person->faxnumber->555-2121
    > $person->address->Big Street[/color]
    The above code will echo the following:
    telnumber is 555-1515
    faxnumber is 555-2121
    address is Big Street

    Hope this was what you're looking for.

    --
    Børge Alvestad
    aka BraveBrain


    Comment

    • Jason Dumler

      #3
      Re: How to get the names of all subobjects?

      René wrote:[color=blue]
      > Hello,
      >
      > My example:
      >
      > $person->telnumber->value
      > $person->faxnumber->value
      > $person->???
      >
      > The names of the objects "telnumber" and "faxnumber" (and further
      > possible subobjects) are not known for me.
      >
      > How can I get the names of all subobjects of an object?
      >
      > Thanks a lot.
      > René[/color]

      If you use the function "get_class_vars ", you can get a list of all the
      properties of the class. Loop through each element of the resulting
      array, and use the "is_object" function to determine if it's an object.

      $class = new Classname();
      $vars = get_class_vars( $class);

      while ( list($name, $val) = each($vars) ) {
      if ( is_object($val) ) {
      print $name . " is an object!\n";
      // to reference the property directly - not sure in how
      // many versions of PHP this works in
      $subclass = $class->$name;
      print_r($subcla ss);
      print "\n";
      }
      }

      Jason Dumler

      Comment

      Working...