-10

Here some example of code

class A {
    public $attribute1;
    public $attribute2;
    public $attribute3;
    ........
    public $attributeN;
}

I need to know how many properties I can have in the class. What say about this PSR or phpmd or something else standard?

With bud example I can have "how many I want", but I need to write it with PSR and phpmd. I am searching this but still can not find.

Thanks for help.

Aram Grigoryan
  • 740
  • 1
  • 6
  • 24
  • what if I say *until your program got struck* – Abdulla Nilam Aug 09 '18 at 06:20
  • 6
    There is no technical limit for this in the strict sense, however you obviously can bring any system into trouble if you generate code that uses say a few billions of attributes and then instanciate millions of such elements. – arkascha Aug 09 '18 at 06:24
  • 1
    What I would like to ask here is: why would you need a huge amount of attributes ("huge" in the sense of "not normal" or "as expected" or "to be handled and understood by a human"). If you are looking for a _dynamic_ set of attributes then that is something else, then you should _not_ code them as static attributes but as a single attributes of a custom type. That custom type for example could implement the ArrayAccess interface to transparently map things as desired. – arkascha Aug 09 '18 at 06:26
  • 1
    As far as I know there is no limit described in the PSR. Even the extended PSR12 does not describe, how many properties a class should have. However classes should be small and functional. If you have a class with this huge number of properties, I guess something went extremely wrong. – Marcel Aug 09 '18 at 06:28
  • Thanks a lot for all comments but I have found the answer – Aram Grigoryan Aug 11 '18 at 09:43
  • 3
    I think your question will only attract opinions, not answers based on hard fact. Your own answer, for instance. Why is the limit 15 fields? Why not 16? If I have a class with 15 fields and I add one more field will I get a severe performance drop? If I have a class with 16 fields and I remove one field will I get a large performance jump? – LBear Aug 14 '18 at 00:20
  • 1
    Can you provide an example code that shows that 15 fields is good and 16 fields is bad? If someone else has an opinion that the limit is 16 fields, that example code will be useful to determine who is right. If you cannot, you have proven yourself that your answer is just an opinion. After examining the link you provided as source, it doesn't seem to have any research to support its own opinion. Your source seems to be just an opinion based on someone else's opinion. – LBear Aug 14 '18 at 00:23
  • @LBear I do not want to promote the product. I just say if you want to become a pro-level programmer you cannot have 16 or 17 attributes. Also about this say phpmd. Your code can review anyone else and can mark your code as not best practise – Aram Grigoryan Aug 14 '18 at 09:03
  • Let's agree to disagree. From their profiles, Abdulla Nilam and arkascha are already pro-level programmers. Their opinions have more weight than both of ours combined. Please seriously consider their comments. I would. – LBear Aug 14 '18 at 11:23
  • Sorry but I do not continue to disagree with you. Here is the phpmd answer https://phpmd.org/rules/codesize.html and section TooManyFields Since: PHPMD 0.1 – Aram Grigoryan Aug 14 '18 at 11:25

2 Answers2

9

Like arkascha said, there is no technical limit for this. Classes are implemented using hash tables and access to its properties (or fields) is designed to work efficiently at constant time for millions of properties. See this page: https://en.wikipedia.org/wiki/Hash_table

PHPMD is a tool for analyzing source code. The rules described in its Rules page including its Code Size Rules are just its default configuration. In other words, you can modify it to whatever you want to match your own policy.

For example, if a company has a policy that the maximum number of fields should be 100, look for the "TooManyFields" rule and modify it like this:

<properties>
    <property name="maxfields" description="The field count reporting threshold " value="100"/>
</properties>

Another company can modify the maximum limit to 50 thousand fields, like this:

<properties>
    <property name="maxfields" description="The field count reporting threshold " value="50000"/>
</properties>

You can even create your own rules.

In conclusion, the rules described there are not for everyone to follow. They are just default configuration. Any company and any development team can modify the configuration to match their own policies.

patrick
  • 183
  • 6
  • You're right. They are just configuration, like the configuration of any software. Not official rules. – LBear Aug 15 '18 at 01:51
  • @LBear Exactly. In the website, there is no claim that they are official rules, not even recommendations. If they were official rules, you'd find them on php.net, not buried inside a documentation of a software's configuration. – patrick Aug 15 '18 at 10:16
-1

If you plan to have attributes in such way, you definitely do it wrong.

public $attribute1;
public $attribute2;
public $attribute3;
........
public $attributeN;

You probably need several arrays, with all that values.

David Makogon
  • 69,407
  • 21
  • 141
  • 189
A. Denis
  • 562
  • 7
  • 15
  • I just show an example `$attribute2` can be an array and $attributeN can be an array. In my code, I have attributes array, but I want to know about the right case – Aram Grigoryan Aug 09 '18 at 06:48
  • no that is not good way when you want to code as an profesional – Aram Grigoryan Aug 09 '18 at 07:24
  • @A.Denis - Please read the new [Code of Conduct](https://stackoverflow.com/conduct). Insults about someone's question or code has no place here. I edited your answer, accordingly. – David Makogon Aug 11 '18 at 06:48