I'm working on a software in which models can have custom fields. This means using a user interface, customers can add and remove fields.
Now, I have a Customer class and I want to fill object values from associative array or JSON. Normally I would do is:
$customer = new Customer();
$customer->first_name = $firstName;
$customer->last_name = $lastName;
.....
What I want is to be able to do like this:
$data = array(
"first_name" => $firstName,
"last_name" => $lastName,
....
);
$customer = getCustomer($data);
and the getCustomer() method should not be dependent on number of entries in the array.
Is this doable in PHP?
I found something like this on searching:
$customer = (object)$data;
Is it correct?
Thanks