This article introduces the usage differences of static, const and define in PHP. Friends in need can refer to it.
In PHP, static, const and define are often used. Today we will learn about the differences between the three? define part: Macros can be used not only to replace constant values, but also to replace expressions and even code snippets. (Macros are very powerful, but they are also error-prone, so their pros and cons are quite controversial.) The syntax of macro is: #define macro name macro value As a suggestion and a common practice among programmers, macro names are often written in all uppercase letters. Advantages of taking advantage of macros: 1) The code is 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) Convenient code maintenance The processing of macros during the compilation process is called "preprocessing". 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 constants defined by const have data types. Constants that define data types facilitate the compiler to check 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: Memory space allocation. 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, such as #define null 0; the compiler always replaces null with 0 when it encounters null. It has no data type (if you have any questions, please look at the preprocessing part of the C language book or look at MSDN. The constants defined by const have data types. Defining constants of data types facilitates the compiler to check data and troubleshoot possible errors in the program. Therefore, the difference between const and define is that defining constants with const eliminates the insecurity between programs. define defines global constants in any Accessible from anywhere const is used for class member variable definition. It can only be accessed by class name and cannot be changed. If it is obvious to beginners, don’t get too carried away. PHP5 has added a lot of object-oriented ideas. PHP5’s object-oriented ideas are closer to Java’s object-oriented ideas. Here we will describe the functions of static and const keywords in PHP5, hoping to be helpful to friends who are learning PHP5. (1) The static 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, and are inaccessible to other classes. Only shared instances of the class can ensure that the program fully protects the members. 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. An example of the above code:
n" ); //Use direct input of class name to access static method Counter::getCount
//Print class version
print( "Version used: " .Counter::VERSION. " |