Home>Article>Backend Development> A direct look at the differences between static, const and define in PHP

A direct look at the differences between static, const and define in PHP

coldplay.xixi
coldplay.xixi forward
2020-08-12 17:19:25 2501browse

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,

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. And const The defined constants have data types. Constants that define data types facilitate the compiler to check data and troubleshoot possible errors in the program. Therefore, the difference between const and define is that const defined constants eliminate the insecurity between programs.

define defines global constants, which can be accessed anywhere. Const is used for class member variable definitions. It can only be accessed with class names and cannot be changed. If it is obvious for beginners, just don’t get too involved. PHP5 has added a lot of object-oriented features. The object-oriented thinking of PHP5 is closer to the object-oriented thinking of Java. 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 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:

The code is as follows:

\n" ); //使用直接输入类名来访问静态方法Counter::getCount //打印类的版本 print( "Version useed: " .Counter::VERSION. "
\n" ); ?>

Related recommendations:

Programming video course

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!

Statement:
This article is reproduced at:jb51.net. If there is any infringement, please contact admin@php.cn delete