Home>Article>Backend Development> A direct look at the differences between static, const and define in PHP
define part:
Macros can not only be used to replace constant values , can also be used to replace expressions or even code snippets. (The function of macro is very powerful, but it is also error-prone, so its pros and cons are quite controversial.)
The syntax of macro is:
#define macro name macro value
as a It is recommended and a common habit among programmers that macro names often use all capital letters.
Related learning recommendations:php programming(video)
##Advantages of using macros:
1) Make the code more concise and clear
Of course, this depends on you giving the macro an appropriate name. Generally speaking, macro names should have clear and intuitive meanings, and sometimes it is better to make them longer.
2) Facilitate code maintenance
The processing of macros is called "preprocessing" during the compilation process. That is to say, before formal compilation, the compiler must first replace the macros that appear in the code with their corresponding macro values. This process is similar to the search and replace you and I use in word processing software. Therefore, when macros are used to express constants in the code, in the final analysis, immediate numbers are used, and the type of this quantity is not clearly specified.const part
The format of constant definition is:const data type constant name = constant value;
The constant defined by const has a data type, Defining constants of data types facilitates the compiler to check the data and troubleshoot possible errors in the program. A constant must initially specify a value, and then, in subsequent code, we are not allowed to change the value of this constant.
The difference between the two:
The allocation of memory space. When define defines a macro, it will not allocate memory space. It will be replaced in the main function during compilation. It is just a simple replacement without any checks, such as type, statement structure, etc. That is, the macro definition constant is just a pure placement. Relationship,
(1) The staticstatic keyword in a class describes that a member is static. Static can restrict external access, because the members after static belong to the class and do not belong to any object instance. , other classes are inaccessible, and only instances of the class are shared, so that the program can protect the members with all their heart. Static variables of a class are very similar to global variables and can be shared by all instances of the class. The same is true for static methods of a class, similar to global functions. Static methods of a class can access static properties of the class. In addition, static members must be accessed using self. Using this will cause an error.
(2) constconst is a keyword that defines a constant, similar to #define in C. It can define a constant. If its value is changed in the program, an error will occur. Give an example of the above code:
\n" ); //使用直接输入类名来访问静态方法Counter::getCount //打印类的版本 print( "Version useed: " .Counter::VERSION. "
\n" ); ?>
Related recommendations:
The above is the detailed content of A direct look at the differences between static, const and define in PHP. For more information, please follow other related articles on the PHP Chinese website!