Why do we need to define a json attribute for the Areturn class again? This attribute is the & quot; json & quot; string. why not directly reference it? There are many examples of classes encapsulated by others I have seen. why? For example, this seems to me something wrong...
Why do we need to define a json attribute for the Areturn class again? This attribute is the string "json". why not directly reference it?
There are many examples of classes encapsulated by others I have seen. why?
For example
This
This gives me the feeling that something wrong has defined the usefulness of these constants !!!! Is it worse to directly use a string?
My colleague said that there is always a reason for Daniel's practice. why is it true?
Reply content:
Why do we need to define a json attribute for the Areturn class again? This attribute is the string "json". why not directly reference it?
There are many examples of classes encapsulated by others I have seen. why?
For example
This
This gives me the feeling that something wrong has defined the usefulness of these constants !!!! Is it worse to directly use a string?
My colleague said that there is always a reason for Daniel's practice. why is it true?
Prevent repeated efforts when modifying the literal volume in the future.
After the CONST constant is defined in the class, you only need to use this constant for calling elsewhere. to modify it, you only need to modify one.
Learn about code refactoring.
What is a constant is a value that will not be modified when you run the program later. It is reasonable to configure a type of data as a constant. It is highly readable and easy to maintain.
If one day you suddenly find that the constant value is unreasonable and needs to be modified, you only need to change one place, which is simpler and more modular.
This is irrelevant to PHP. constants are used on different platforms in different languages.
Above
For readability, but this will reduce the performance, because we need to query the symbol table twice.
The one in the first figure is a bit redundant. you can use "json" directly. the advantage of defining constants is that it is restricted. for example, if someone is not clear, it will write "JSON"
After using the definition in the second figure, it is quite elegant. Otherwise, it is all numbers. you will not know what these numbers are for in the future.
1. Defining constants for numbers is helpful for reading.
If ($ count> 20)
If ($ count> MAX_POST)
2. it is friendly to Refactoring. you only need to modify the constant and change it to all places. Otherwise, you need to find 20 and change it to 30.
3. Text constants are error-friendly
Define ('foobar', 'foobar ');
Fn (FOOBAZ); // error. the constant does not exist.
Fn ('foobaz'); // The spelling error will not be prompted during compilation unless the fn parameter is checked to be equal to foobar
Finally, everything has a degree, and the degree of control is an empirical problem.
It's easy. it's common in all programming languages.
Define a constant or macro.
1. code semantics can improve code readability, such as writingTINKINGIMAGE_GD
It is always better to read than write 1, and it is not prone to errors
2. if you need to modify this constant, just change one place.
For example, if "json" is changed to "json" in the future ".
1. improve readability. for example, NEWS: STATUS_DETETED is deleted.
2. IDE automatic prompt
3. convenient modification
Personal experience:
In the past, the normal state value was 0, which was scattered in every corner of the program. Then, in order to unify the state expression mode with other system models, change it to 1 as normal.
It took a long time (several hours, but few days after the event, it was not found ). This is not required if you define constants.