$class = new Class;
$foo = json_decode($_POST['array']);
In this highly contrived example, I have a class with its own functions and variables, blah blah.
I also just decoded a JSON string, so those values are now in$foo. How do I move the elements in $foo over to $class, so that:
$foo->name becomes $class->name?
Would be trivial if I knew what all the elements were, yes… except for the sake of being dynamic, let’s say I want them all transferred over, and I don’t know their names.
You could use
get_object_vars:You could also implement this in your class:
And then cast the object into an array:
or add a method to do that too: