简体   繁体   中英

Method chaining with variable variables

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

$incident = new stdClass();
foreach($data as $key=>$chain){
  $str = "{'".implode("'}->{'",$chain['parts'])."'}";
  eval("@\$incident->$str = '$chain[value]';");
}
print_r($incident);

Live demo : https://eval.in/923232

OUTPUT is as

stdClass Object
(
    [CustomFields] => stdClass Object
        (
            [c] => stdClass Object
                (
                    [make] => Some value
                )

        )

    [StatusWithType] => stdClass Object
        (
            [Status] => stdClass Object
                (
                    [ID] => 34
                )

        )

)

and now you can easily access like $incident->CustomFields->c->make

@kranthi is technically right( in the comment ), I given the implementation.

So, kranthi was on the right track.

$incident = new Incident();
foreach($data as $array)
{
   $this->setDynamicFields($incident, $array['parts'], $array['value']); 
}
$incident->save();

function setDynamicFields($obj, $parts, $value)
{
   if(is_array($parts) && count($parts) == 3)
   {
       $obj->{$parts[0]}->{$parts[1]}->{$parts[2]} = ($parts[0] == 'StatusWithType' ? (int) $value: $value);
   }
}

The trick was to pass the whole $incident object as a function argument (I think that's called as dependency injection if I'm not wrong) and using -> as a literal instead of a string that resided inside a variable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM