According to @Faiz's answer (which I accepted as a formal answer to my question), I created the following example.
If I had a Customer class:
class Customer { public $firstname; public $lastname; public $country; public $gender; ... }
and an HTML web form with INPUT / SELECT fields named 'firstname', 'lastname', 'country', 'gender' ...
<form action="..." method="post"> <input type="text" name="firstname" value="" /> <input type="text" name="lastname" value="" /> <select name="country"> <option value="AL">Albania</option> <option value="BE">Belgium</option> <option value="HR">Croatia</option> ... </select> <input type="radio" name="gender" value="M" />Man <input type="radio" name="gender" value="F" />Woman ... <input type="submit" value="Submit" /> </form>
usually in my script action I will map these form fields to class variables one at a time like:
$Customer=new Customer(); $Customer->firstname=$_POST['firstname']; $Customer->lastname=$_POST['lastname']; $Customer->country=$_POST['country']; $Customer->gender=$_POST['gender']; ... $Customer->create();
But using variable variables, I can easily map all the values ββof associative arrays (there may be many of them that are prone to errors) to class variables using the following foreach loop for the next line:
$Customer=new Customer(); foreach($_POST as $key=>$value) $Customer->$key=$value; $Customer->create();
Note. For simplicity and clarity of the answer (logic), I omitted the sanation of the values ββof $ _POST.
source share