D
D
doexec2019-05-01 22:52:57
PHP
doexec, 2019-05-01 22:52:57

How to name variables logically and structurally?

Good afternoon, colleagues.
The question as a whole concerns not only PHP, but still ...
Tell me, more experienced colleagues, how do you name variables ? By what principle or algorithm?
In the draft script, on line 200, instead of $homepage_product_name, I try to refer to $product_name_home, and so on.
The more specific I name a variable, the more variations in my head as the number of lines in the document grows...
Can someone share their naming scheme? Preferably with a description...
Thank you!

Answer the question

In order to leave comments, you need to log in

5 answer(s)
D
dollar, 2019-05-02
@dollar

In English, nouns at the beginning turn into adjectives, and at the end - the noun to which they refer.
Compare: "stone wall" (stone wall) and "wall stone" (stone from the wall).
That is, the first thing you need to decide on the main noun, that is, WHAT is meant. If it is a name, then the name of the variable must end in _name (or Name, depending on the style).
Further, if you do not find fault with the rules, then the sequence of adjectives is better done like this: the more important, the closer to the noun.
Step one: product_name
Step two: homepage_product_name (it should be obvious to you that this is not home_page)
Further, there should be nothing superfluous, and should reflect the essence as accurately as possible. For example, a setting that says that notifications can only be sent when the application is inactive.
Not correct: notify_if_inactive (not clear at all, not logical)
Correct: is_notify_if_inactive_only The is_
prefix in this case is a convention in our command indicating that it is a boolean variable, you can also use b_ for the same purpose. Or, say, if it is a constant or changes very rarely, you can write it in caps or add the prefix c_
In general, subtleties and tastes already begin. The basic principles are set out above - this is a clear order of construction and compliance with the essence.

T
ThunderCat, 2019-05-01
@ThunderCat

Tell me, more experienced colleagues, how do you name the variables?

As close to natural language as possible. And it is also desirable, taking into account the recommendations for naming standards adopted in a particular language, $homepage_product_name ==> $nameOfProductOnHomepage, although why there is a homepage and how it differs from not on a homepage is a mystery.

A
Alexander, 2019-05-01
@SmoKE_xD

I try to do it according to this principle - $module_function_property

S
Stalker_RED, 2019-05-02
@Stalker_RED

https://m.habr.com/en/post/437122/
There are dozens of articles like this one.
The meaning is not entirely clear. Does the same product have a different name on other pages?

K
klunker, 2019-09-25
@klunker

Names do whatever you want, the main thing is to be clear. And already how to use long names, so use normal IDEs with autocomplete (plus the MVC pattern). In general, you don’t need to fence your garden in the documentation, read about the PSR-0, PSR-1, PSR-4 standards

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question