Parsing the differences between static,const and define in PHP _php tutorial

Source: Internet
Author: User
Tags constant definition define null
Define section:
Macros can be used instead of constant values, and can be used instead of expressions, even code snippets. (Macros are powerful, but error-prone, so their pros and cons are quite controversial.) )
The syntax for a macro is:
Macro value #define Macro name
As a recommendation and a common practice for a broad range of programmers, macro names often use all uppercase letters.
Advantages of using macros:
1) make the code more concise and clear
Of course, it depends on you taking a proper name for the macro. In general, the name of a macro is more important to have a clear and intuitive meaning, sometimes it is better to make it longer.
2) Convenient code maintenance
The processing of macros is referred to as "preprocessing" during compilation. In other words, before the formal compilation, the compiler must first put the code appears in the macro, with its corresponding macro value substitution, this process is a bit of you I in the word processing software in the search and replace. So using a macro to express a constant in your code, in the final analysis, uses an immediate number and does not explicitly specify the type of the quantity.

Const section
The format for a constant definition is:
Const data type constant name = constant value;
Constants defined by const have data types, and constants that define data types make it easy for the compiler to perform data checks, which can cause errors in the program to be checked. Constants must specify a value at the beginning, and then, in future code, we are not allowed to change the value of this constant.

The difference between the two:
Allocation of memory space. Define the macro definition, the memory space will not be allocated, the compilation will be replaced in the main function, just a simple substitution, no checks, such as type, statement structure, that is, the macro definition constant is purely a placement relationship, such as # define NULL 0; The compiler always uses 0 instead of NULL when it encounters null it has no data type (see the Pre-Processing section or MSDN for a C-language book). Whereas const-defined constants have data types, constants that define data types make it easy for the compiler to check for data, which can cause errors in the program to be checked. So the difference between const and define is that the const definition constant excludes the unsafe between programs. Define defines global constants that can be accessed everywhere

Const is used for class member variable definitions and can only be accessed with the class name can not be changed if the beginner so understand the line do not get too close to the line PHP5 many object-oriented ideas, PHP5 object-oriented approach to Java object-oriented thinking. Here we describe the role of static and const keywords in PHP5, hoping to help friends who learn PHP5.

(1) staticstatic keyword in a class is, describes a member is static, static can restrict the external access, because the static member is a class, is not part of any object instance, other classes are inaccessible, only the instance of the class is shared, The member can be protected by certain procedures. The static variables of a class, very similar to global variables, can be shared by instances of all classes, and the static methods of classes are the same, similar to global functions. The static method of the class can access the static properties of the class. It is also stated that the static member must be accessed using self, and using this will cause an error.

(2) Constconst is a keyword that defines a constant, similar to # define in C, that defines a constant, and an error occurs if its value is changed in the program. Illustrate the code above:
Copy CodeThe code is as follows:
Class Counter
{
private static $count = 0;//defines a static property
Const VERSION = 2.0;//defines a constant
constructor function
function __construct ()
{
Self:: $count + +;
}
Destructors
function __destruct ()
{
Self:: $count--;
}
To define a static method
static function GetCount ()
{
Return self:: $count;
}
}
Create an instance
$c = new Counter ();
Perform printing
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 ");
?>

http://www.bkjia.com/PHPjc/327646.html www.bkjia.com true http://www.bkjia.com/PHPjc/327646.html techarticle define: A macro can be used instead of a constant value, and it can be used instead of an expression or even a code snippet. (macro function is very powerful, but also error-prone, so the pros and cons of the size is quite ... )

  • Related Article

    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.