Home >Backend Development >PHP Tutorial >Analyze the differences between static, const and define in php_PHP tutorial

Analyze the differences between static, const and define in php_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:06:08955browse

define part:
Macros can not only be used to replace constant values, but also to replace expressions and even code segments. (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 It is recommended and a common habit among programmers that macro names often use all capital letters.
Advantages of taking advantage of 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) Convenient 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 Data type, defining the constants of the data type 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:
In 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. 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 It can be accessed anywhere.

const is used for class member variable definitions. It can only be accessed by class name and cannot be changed. If it is obvious to beginners, it is ok. Just don’t get too carried away. PHP5 has added a lot of object-oriented ideas. PHP5 is oriented to Objects are closer to Java's object-oriented thinking. Here we describe the functions of static and const keywords in PHP5, hoping to be helpful to friends who are learning PHP5

(1) The static keyword is in a class. , describes a member as static. Static can restrict external access, because the members after static belong to the class and do not belong to any object instance. They are inaccessible to other classes and are only shared with instances of the class. This can ensure that the program This member is fully protected. The static variables of the class are very similar to the global variables and can be shared by all instances of the class. The same is true for the static methods of the class. The static methods of the class can access the static properties of the class. The thing is, 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. For example, the above code:

Copy the code The code is as follows:

class Counter
{
private static $count = 0;//Define a static property
const VERSION = 2.0;//Define a constant
//Construction Function
function __construct()
{
self::$count++;
}
//Destructor
function __destruct()
{
self:: $count--;
}
//Define a static method
static function getCount()
{
return self::$count;
}
}
//Create an instance
$c = new Counter();
//Perform printing
print( Counter::getCount(). "
n" ); //Use direct Enter the class name to access the static method Counter::getCount
//Print the version of the class
print( "Version used: " .Counter::VERSION. "
n" );
?> ;

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327646.htmlTechArticledefine section: Macros can not only be used to replace constant values, but also to replace expressions and even code snippets . (Macros are very powerful, but they are also easy to make mistakes, so they have pros and cons...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn