将客户地址原语封装到类中


Wrapping customer address primitive into a class

我使用对象注入将客户地址作为对象传递给类,而不是传递数组。我的问题是关于客户的地址对象,设置setter和getter的最佳方法是什么?

我确实考虑过使用这样的东西:

public function __call() {
 // search for set or get and act accordingly or throw an exception.
}

但是,我觉得它可能太模糊了。我应该自己做胡瓜吗?

public function setFirstName();
public function setLastName();
public function setCompany();
public function setAddressLine1();
public function setCity();
public function setZipCode();
public function setPhone();
public function setCountry();
// then do the same for the get methods.

或者我可以将一些方法与__call(魔术方法)和一些声明的方法结合起来?

请告诉我什么是最好的方法或替代方法,以及为什么。

提前感谢您抽出时间,

经验法则:喜欢显式而不是隐式。因此,显式setter和getter通常是首选样式。

这样看:getter和setter只以其原始形式移交有效负载,尽管如果需要,它们可能会做更多的工作:过滤、验证、修复、增强。。。但最重要的是:它们的存在是类期望并授予用户获取或设置值的明确标志。而有了这些"神奇的getter和setter",你就会盲目地飞翔。IDE也是如此:现在他们的自动完成可以和他们一起工作了。所以要保持风格和明确。其他人会为此感谢您:-)