PHP评估类子成员

class Customer{
    public $country;
    public function __construct(){
      $this->country = new Country();
      $this->country->name = "USA";
   }

}

class Country{

}

I want to print the country name, but i have the following context only.

 $c = new Customer();
 $member = "country.name";// i cant decide on this, this is what i get as a parameter

Can you please suggest how I can print the country name with the provided country.name string?

The solution im building here is for email template

"the country that good for you is {country.name}"

So I will replace country.name to USA with the help of $c and $member

You can achieve this by iterating over the elements in your $members string, and stepping further into the object each time:

// First initialise our reference to the "top-level" object
$ref = $c;
// Break apart our steps
$steps = explode('.', $members);

// For every element in the array, dig deeper into the object hierarchy
while ($step = array_shift($steps)) {
    $ref = $ref->{$step};
}

// Once the loop is finished, $ref will be set to $c->country->name
echo $ref;

See https://eval.in/954722