【发布时间】:2017-03-23 18:56:31
【问题描述】:
我有对象$customer
我需要使用联系信息创建新对象$contact
有什么更好的方法来创建它?
/** the first way */
$contact = (object) array('name' => $customer->name, 'phone' => $customer->phone, 'email' => $customer->email);
/** the second way */
$contact = new stdClass();
$contact->name = $customer->name;
$contact->phone = $customer->phone;
$contact->email = $customer->email;`
【问题讨论】:
-
第二种方式当然更容易阅读。
-
第二,因为不需要转换
-
$contact = clone $customer;怎么样?