> -----Original Message----- > From: 肖羗 [mailto:xiaohan2012@xxxxxxxxx] > Sent: Thursday, November 25, 2010 12:21 AM > To: php-general@xxxxxxxxxxxxx > Subject: any way to iterate the fields in a class > > Actually, what I am seeking is how to assign values to the fields in a class via > an array. > I have tried like this. However failed. > I have a class. > *class book{ > var name;* > *var price;* > *}* > * > * > And I have got an array. > > *$array=array('name'=>'harry potter','price'=>'$122');* > > By using function *extract(), *I assign the values to $name and $price while > not $this->name and $this->price, which is not what I want. > > So I am thinking iterating the fields in an array and assign the values each by > each through an array. > > Is there any way to assign values to the fields in a class via an array. > > Thanks in advance! Look into reflection [1]. Or you can just $bookObject = (object) $array; Note: $bookObject != new BookClass(). Regards, Tommy [1] http://www.php.net/manual/en/book.reflection.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php