dropbox1111 2015-11-21 10:03
浏览 17
已采纳

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

I'm passing in a customer's address as an object to a class using object injection instead of passing an array. My question is on the customer's address object what would be the best method for setting up the setters and getters?

I did think about using something like this:

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

but, i thought that it might be too vague. Should I just make the getters myself?

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.

Or maybe I could combine some methods with __call (magic methods) and some declared methods?

Please can you let me know what might be the best method or alternative way of doing this and why.

Thank you in advance for your time,

  • 写回答

1条回答 默认 最新

  • duanpuchun5275 2015-11-21 18:51
    关注

    As a rule of thumb: prefer explicit over implicit. So explicit setters and getters usually are the preferred style.

    Look at it this way: getters and setters only hand over a payload in their primitive form, though they may do much more if required: filtering, validating, fixing, enhancing, ... But above all: their existence is a clear sign the class expects and grants a user to get or set a value. Whereas with these "magical getters and setters" instead you fly in the blind. Same for IDEs: now way their auto completion can work with them. So keep it in style and explicit. Others will thank you for it :-)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 qgcomp混合物线性模型分析的代码出现错误:Model aliasing occurred
  • ¥100 已有python代码,要求做成可执行程序,程序设计内容不多
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答
  • ¥20 在本地部署CHATRWKV时遇到了AttributeError: 'str' object has no attribute 'requires_grad'