Good afternoon, colleagues.
The question as a whole concerns not only PHP, but still...
Tell me, more experienced colleagues, how do you name variables? What principle or algorithm?
In the draft script in line 200, instead of $homepage_product_name, I try to access $product_name_home, etc.
The more specifically I name a variable, the more variations in my head as the number of lines in a document increases...

Can someone share their naming scheme? It is desirable with the description...
Thank!
  • homepage_product_name

    Homepage product name?
    Name of goods on the main?

    The meaning of this variable is not clear, but it should be clear
    – Gleaming Guanaco May 1 '19 at 22:58
  • Gleaming Guanaco is the name of the product on the main page.
    The meaning of the variable name is clear from the moment it came up, and until it became distracted by deeper jungles...
    Therefore, I am looking for the very"core", which is worth adhering to.
    – Surprise4 May 1 '19 at 23:15

4 Answers 4

I try to do it like this - $module_function_property
  • Can you give an example with a description? – Surprise4 May 1 '19 at 23:16
  • [[doexec]], $shop_basket_product or $blog_article_title.
    In general, correctly written above, it's time to move on to the PLO
    – Terrible Pogo May 1 '19 at 23:24
  • [[SmoKE_xD]], switching to OOP will not help a person in any way.He asks how to properly learn to name variables and properties.OOP will not do it for him.
    Normal code to write and normally name the methods/variables is also possible in the procedure.
    – Grimy31 May 1 '19 at 23:41
  • [[Mysterion]],
    switching to OOP will not help a person in any way.
    wrong statement, as I already wrote - the very splitting of the code into logically nested structural blocks and objects greatly simplifies the naming of entities, if the procedural style is the same $homepage_product_name xs what it is and where, then the op will be in IndexController :: indexAction() and look like $product->name, which uniquely relates it to the main page and the name of the product, and an order of magnitude shorter and absolutely logical.
    – Funky58 May 2 '19 at 00:03
  • [[ThunderCat]], it does not depend on OOP, but on the developer.
    You can also create a controller CategoryController, and there will be categories of news, categories of articles and categories of goods in the store.
    – Grimy31 May 2 '19 at 01:31
  • [[Mysterion]], Who argues, d ** can be decoded on any architecture, but the OOP structure is more logical initially, if the developer is not a round idiot unable to separate the soft from the warm, then no architecture will save, but I do not think that categorical the statement “switching to the PLO will not help a person in any way” is justified;first, you don’t know a person personally to judge his mental abilities, right? And secondly - I do not see a contradiction - even in the example you cited, where everything is piled up, structural organization allows you to reduce entity naming and build logic(albeit perverted and curved), the essence is all the same - the code is more structurally divided and entities are logically named succinctly and without redundancy. – Funky58 May 2 '19 at 02:30
  • [[ThunderCat]], there you also need to invent names for properties.The author has a question “how to think out correctly”.He will have the same question with any architecture.
    It's never easier to make names in OOP.
    When you say something, remember that the question is not a programmer.
    – Grimy31 May 2 '19 at 12:12
  • [[Mysterion]],
    When you say something, remember that the question is not a programmer.
    Where does this conclusion come from?
    Tell me, more experienced colleagues, what do you call variables?
    A person is clearly positioning himself as a programmer, that he is inexperienced is another matter, but this does not apply to the question.
    there you just need to invent property names.
    Yes, but invent names properties are a completely different task than coming up with names of hundreds of variables from spaghetti code, since there is no structural nesting, and the properties are usually more natively named, the name will be in the product and in the user and in other entities, while these are different"names".
    Compare: $homepage_product_name and $product->name called in index controller :: index action, you can’t write here $name->product, there is a clear sequence.
    – Funky58 May 2 '19 at 12:28
  • [[ThunderCat]],
    $product_name
    $product->name
    I do not see the difference.Why do you compare the obviously abnormal code with the code that the author does not have?
    The author has a code that is not convenient to read.You also say that switching to OOP will solve all his problems with this.
    A clear sequence is not in the PLO, it should be in my head.
    You can write like this:
    $product->product_name
    – Grimy31 May 2 '19 at 12:47
  • [[Mysterion]],

    $product_name
    $product->name
    I do not see the difference.
    ...
    A clear sequence is not in the PLO, it should be in the head.
    It’s just not necessary to consider everyone as an idiot, when a person in noodles tries to deduce some sort of naming structure, this indicates a lack of structure, not a lack of brains.
    – Funky58 May 2 '19 at 12:56
  • [[ThunderCat]], I don’t consider everyone to be idiots.There is no lack of structure without OOP.You’ve figured it out yourself.Structuring the code, its convenience and beauty has nothing to do with OOP.
    You are attached to it as if the PLO is a panacea for all"diseases" in programming.
    – Grimy31 May 2 '19 at 14:09
  • [[Mysterion]],
    Structuring code, its convenience and beauty has nothing to do with OOP.

    In general, the main role of OOP is code structuring, initially even to the detriment of performance.

    You clung to it as if OOP is a panacea for all"diseases" in programming.
    No, of course A silver bullet does not exist, but a normal programmer should know and be able to do in OOP.
    – Funky58 May 2 '19 at 14:43
Prompt, more experienced colleagues, how do you call variables?

As close as possible to the natural language.And it is also desirable, taking into account the recommendations for naming standards adopted in a particular Jap, $homepage_product_name ==>$nameOfProductOnHomepage, although why there is a homepage and what's different from not homepage is a mystery.
  • Well, I took an example out of my head right now, so that there was something to disassemble in the question, so Homepage is abstract here.We can estimate that this product is just"featured", recommended and displayed on the main page.Let it be so.

    With such a variable name length($nameOfProductOnHomepage), I will get entangled in them much faster....
    – Surprise4 May 1 '19 at 23:19
  • [[doexec]], if you are in the main page controller, then in this context there is only the name of the product, there are no other names for the product, there are no other pages

    That is, here is the name of the product is the name of the variable $name
    And here is the friend, but the name is also $name

    That is an example - the name of a person:
    at the institute is the name of the student
    in the hostel is the name of the tenant
    just like that - the name of a citizen

    Like it or not, in a single context, the name is
    – Gleaming Guanaco May 1 '19 at 23:26
  • [[doexec]], you will be confused in your own, I just weakly imagine really long naming in a structured project with objects as the basis of development.As I wrote above[[Maksclub]] - the very structural decomposition into objects will no longer allow naming In MVC, you will break everything down into a controller + action + objects, and inside the code it will be like:

    =code class"IndexController extends Controller {
    ...
    public function indexAction() {
       $product=new Product();
       $product->initById(55);
       echo $product->name;//although they don’t do so
    }
    ...
    }
    – Funky58 May 1 '19 at 23:54
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 to do is to decide on the main noun, that is, WHAT is meant.If this is the name, then the variable name should 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: the more important, the closer to the noun.
Step One: product_name
Step two: homepage_product_name(it should be obvious to you that it is not home_page)

Further, there should be nothing superfluous, and it should reflect the essence as precisely as possible.For example, a setting that says that notifications can be sent only if the application is inactive.
Not right: notify_if_inactive(not at all clear, not logical)
That's right: is_notify_if_inactive_only
The is_ prefix in this case is an agreement in our team, indicating that it is a boolean variable, you can also use b_ for example for the same purpose.Or, say, if it is a constant or changes very rarely, you can write a caps or add the prefix c_

In general, the subtleties and tastes begin further.The main principles are set out above - this is a clear order of construction and relevance to the essence.
https://m.habr.com/en/post/437122/
There are dozens of articles like this.
homepage_product_name

The meaning is not quite clear.On other pages, does this product have a different name?