dongyuan1983 2014-05-04 14:56
浏览 110
已采纳

我是否需要声明PHP对象中的每个属性

I've understood the basic concept of OOP for a while and have used it a little here and there, but decided that this project would be fully OOP. I'm working with PHP.

Do I need to declare every single property of an object? They seem to work without the declaration, but is it best practice to make the declaration anyway. That could turn into a very long list very quickly.

If not all properties need to be declared, then which ones would and which ones wouldn't.

I'm already up to this, and I have barely scratched the surface. There are dozens more properties to come:

public $api_key;
public $api_url;
public $data;
public $temp_scale;
public $longitude;
public $latitude;
public $degsymbol;
public $temp;
public $time;
  • 写回答

1条回答 默认 最新

  • duanping6698 2014-05-04 23:16
    关注

    You should declare only the public properties which define the concept represented by the object. This means that first you need to define WHAT is the single responsibility of the object. Then expose the properties you want used by other objects.

    Don't expose internal details i.e things that another object shouldn't care. If you just want to define a data structure (only fields, no behaviour) use directly an array it's easier.

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

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图