Analysis of static, const, and define in php _ php skills

Source: Internet
Author: User
Tags constant definition define null
This article provides a detailed analysis of the differences between static, const, and define in php. For more information, see Define section:
Macros can be used not only to replace constant values, but also to replace expressions or even code segments. (Macros have powerful functions but are prone to errors, so their advantages and disadvantages are quite controversial .)
Macro syntax:
# Define macro name macro value
As a suggestion and a common habit of programmers, macro names often use uppercase letters.
Advantages of macros:
1) make the code more concise and clear
Of course, this depends on a proper name for the macro. Generally, the macro name should be more explicit and intuitive, and sometimes it is better to keep it longer.
2) easy code maintenance
Macro processing is called "preprocessing" during compilation ". That is to say, before official compilation, the compiler must replace the macros in the code with their corresponding macro values. in this process, you may find and replace them in the text processing software. Therefore, when macro expressions are used in the code, the immediate number is used in the final analysis, and the type of this number is not explicitly specified.

Const section
Constant definition format:
Const data type constant name = constant value;
The constants defined by const have data types. constants defining data types are convenient for the compiler to perform data checks, so that programs may encounter errors for troubleshooting. A constant must be specified at the beginning. Then, in future code, we cannot change the value of this constant.

Differences between the two:
Memory space allocation. When define is used for macro definition, no memory space is allocated. during compilation, it will be replaced in the main function, but it will be replaced simply without any check, such as the type and statement structure, that is, macro-defined constants are purely placement relationships, for example, # define null 0; when the compiler encounters null, it always uses 0 instead of null. it has no data type. (For more information, see C language books for preprocessing or MSDN. the constants defined by const have data types. constants defining data types are convenient for the compiler to perform data checks, so that programs may encounter errors for troubleshooting, therefore, the difference between const and define is that const defines constants to eliminate the security of programs. define defines global constants and can be accessed anywhere

Const is used to define class member variables. it can only be accessed by class names and cannot be changed. if you are a beginner, you can simply understand it. do not be too arrogant. PHP5 has added a lot of object-oriented ideas, PHP5's object-oriented approach is closer to Java's object-oriented approach. Here we will describe the role of the static and const keywords in PHP5, and hope it will be helpful for friends who want to learn PHP5.

(1) the staticstatic keyword is in the class, describing a member is static, static can restrict external access, because the static member belongs to the class, it does not belong to any object instance, and other classes cannot be accessed. it is shared only to the instance of the class and can be fully protected by the program. Static variables of the class are very similar to global variables and can be shared by all class instances. static methods of the class are also the same, similar to global functions. The static method of the class can be used to initialize the static attributes of the class. In addition, static members must use self for access. if this is used, an error occurs.

(2) constconst is a key word that defines constants. similar to # define in C, constconst can define a constant. if its value is changed in the program, an error will occur. The above code is illustrated as an example:

The code is as follows:


Class Counter
{
Private static $ count = 0; // defines a static attribute
Const VERSION = 2.0; // define a constant
// Constructor
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 ();
// Execute print
Print (Counter: getCount ()."
\ N "); // use the direct input class name to access the static method Counter: getCount
// Print the version of the class
Print ("Version useed:". Counter: VERSION ."
\ N ");
?>

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.