Some strange lines in PHP! Why? -Php Tutorial

Source: Internet
Author: User
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.

  1. 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.

  2. 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.

  3. 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.

  1. 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"

  2. 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_GDIt 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.