I thought more examples might be useful someone.

<?php

class foo {
 var $v1 = 1;
 var $v2 = 2;
 var $v3 = 3;
}

$obj = new foo;

$property_names = array_keys(get_object_vars($obj));

foreach ($property_names as $name) {
 print("foo->$name = ".$obj->$name."<br>\n");
}

while (list($k,$v) = each($obj)) {
 print("foo->$k = ".$v."<br>\n");
}

foreach ($obj as $k => $v) {
 print("foo->$k = ".$v."<br>\n");
}

foreach ($obj as $k => $v) {
 print("foo->$k = ".$obj->$k."<br>\n");
}

?>

Regards,

--
Yasuo Ohgaki


"André Næss" <[EMAIL PROTECTED]> wrote in message
[EMAIL PROTECTED]">news:[EMAIL PROTECTED]...
Javascript has this very neat control structure that makes it easy to
iterate over the properties of an object. It would be nice to see something
similiar in PHP. I often use Objects as a way of "packing" data in a more
orderly fashion, and sometimes I need to do stuff to all the properties of
an object. A good example is when I have an object whose properties are
coming from a form, and going back to the very same form if there were
errors in the form. I will have to perform addslashes() and stripslashes()
on all the properties in the object, and writing this without a loop can be
very tedious.

André Næss

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to