如何将变量附加到魔术方法PHP?

how to append variable to magic method in PHP?

i have object like this return $profile->hourly_rate; and i want to make this hourly in hourly_rate dynamic depends on my variable, here the example:

$types = ['hourly', 'daily', 'weekly', 'monthly'];

How i can do that? Thanks.

It's better to use property like rates which will be array. Then you can access required value as:

$rate_type = 'hourly';
echo $profile->rates[$rate_type];

In case it is not possible use this code:

$rate_type = 'hourly';
echo $profile->{$rate_type . '_rate'};

Mmmmm We don't know what exactly you want. But maybe is something like that?

$profile = new class {
    public function setValueType($value, $type){
        $this->{$type."_rate"} = $value;
    }
};

$profile->setValueType(10, "hourly");

echo $profile->hourly_rate;