I have one object being the result of a database query, looking something like this (var_dump output):
object(stdClass)#17 (6) {
["id"]=>
string(1) "1"
["title"]=>
string(20) "Some Title"
["description"]=>
string(41) "Some really good stuff. Description here."
["slug"]=>
string(19) "some-slug-url"
["picture_url"]=>
NULL
["price"]=>
string(4) "5.99"
}
I just need all property values “transferred” to a different class which has the same property names. Is there a simple way to do this?
Take a look at PHP’s
get_object_vars()-function to achieve the desired effect without tons of assignment statements:You should make sure that you add sufficient error-checking to this, depending on your needs.