When overriding a previously defined variable in PHP, NetBeans recommends as a warning that the variable should be defined only once. Is there any other reason to follow this recommendation, other than this "improved readability"?

    1 answer 1

    I would not recommend overriding a variable at all, but using it only for what it was created for. Imagine, you defined some $users variable in which a collection of users registered in the system is recorded, and then you decided to override it by writing the id array of users you want to delete into it. As a result, there is confusion when reading your code. I think it will not be difficult to create another variable.

    PS If I understand your question correctly

    • You understood everything correctly. If we consider the case that you cited as an example, then I agree with you, but it happens that we perform a chain of transformations over a variable. For example, we take the value of the phone number from the input field, then remove the extra spaces, then do something else with it. In this case, if at each stage to define a new variable, these very variables will become very much. - Side Gleb
    • one
      Yes, use the scope of the variable, create separate methods in which you define your new variables, then there will be no confusion beyond the boundaries of this method. For example, $phone = $this->cleanPhone(); function cleanPhone() {$phone = $this->getPhone(); return $this->clean($phone);} function cleanPhone() {$phone = $this->getPhone(); return $this->clean($phone);} - Ilya Savich
    • And I recommend reading the book by Robert Martin "Clean Code". Simple enough and very informative book - Ilya Savich
    • one
      Well, I read, I strive for clean code. - Lateral Gleb