【发布时间】:2010-12-06 10:46:06
【问题描述】:
不太擅长用文字表达这个问题......
我正在使用 API 来更新一些客户信息
$customer = new Customer();
$customer->first_name = $value;
$customer->update();
上面会更新first_name,但是我想改变名字的地方:
$attribute = 'first_name';
$customer = new Customer();
$customer->$attribute = $value;
$customer->update();`
然后它将引用 Customer 类中的公共变量。
我想避免为每个属性编写一个方法(setter)。
【问题讨论】:
-
以上方法你试过了吗?管用吗?如果是这样,问题是什么?
-
$customer->{$attribute} = $value;
-
你的问题可能是
$attrubute的拼写错误。 -
(参考) Variable variables
-
我想说您的问题与您提供的代码无关。您能否提供一个完整的工作示例或向我们展示
var_dump($customer)?