By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,664 Members | 1,656 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,664 IT Pros & Developers. It's quick & easy.

How to get the names of all subobjects?

P: n/a
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Ú
Jul 16 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

"RenÚ" <go****@teinze.com> wrote in message
news:56**************************@posting.google.c om...
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Ú
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 $person->telnumber->555-1515
$person->faxnumber->555-2121
$person->address->Big Street

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
Jul 16 '05 #2

P: n/a
RenÚ wrote:
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Ú


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($subclass);
print "\n";
}
}

Jason Dumler

Jul 16 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.