The API that I'm using (ConnectPHP of Oracle Service Cloud) follows the chaining approach. For example:
$incident = new Incident();
$incident->CustomFields->c->make = "Same value";
$incident->StatusWithType->Status->ID = 34;
$incident->save();
How would I go about achieving the same if the subsequent properties of the $incident object are generated dynamically? For example:
$data = array();
$data[0]['parts'] = array('CustomFields', 'c', 'make');
$data[0]['value'] = "Some value";
$data[1]['parts'] = array('StatusWithType', 'Status', 'ID');
$data[1]['value'] = 34;
$incident = new Incident();
foreach($data as $array)
{
foreach($array['parts'] as $key)
{
// how will I generate
// (1) $incident->CustomFields->c->make = $array['value']
// (2) $incident->StatusWithType->Status->ID = $array['value']
}
}
$incident->save();
What I tried
$incident = new Incident();
foreach($data as $array)
{
$parts = implode('->', $array['parts']);
$incident->{$parts} = $array['value']; // this doesn't work even though $parts is coming out with the expected pattern because I think it is converting it into a string representation
}
$incident->save();
If there is no risk of user input , you can create string of all object keys and use eval like this
Live demo : https://eval.in/923232
OUTPUT is as
and now you can easily access like
$incident->CustomFields->c->make@kranthi is technically right(in the comment), I given the implementation.