Re: For ... in ...

From: Date: Thu, 29 Mar 2001 10:13:07 +0000
Subject: Re: For ... in ...
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-46066@lists.php.net to get a copy of this message
You can use get_class_vars() or get_object_vars() http://www.php.net/manual/en/function.get-object-vars.php http://www.php.net/manual/en/function.get-class-vars.php You also need to use Variable variables (i.e. $$var) to iterate object properties. <?php class foo { var $v1 = 1; var $v2 = 2; var $v3 = 3; } $bar = new foo; $property_names = array_keys(get_object_vars($bar)); foreach ($property_names as $name) { print("foo->v1 = ".$bar->$name."<br>\n"); } ?> Regards, -- Yasuo Ohgaki "André Næss" <andre@switch.no> wrote in message news:E13BBFD5DA06D411ADC600508BC25BF7150007@switch01.switch.no... 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: php-general-unsubscribe@lists.php.net For additional commands, e-mail: php-general-help@lists.php.net To contact the list administrators, e-mail: php-list-admin@lists.php.net

« previous php.general (#46066) next »