質問

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;
役に立ちましたか?

解決

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top