Analysis of the difference between static,const and define in PHP

Source: Internet
Author: User
Tags define null php class
This article introduces, PHP in the Static,const and define usage difference, the need for friends to refer to it.

In PHP, often use static,const and define, today we come to understand the difference between the next three what?

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. (Macros are powerful, but error-prone, so their pros and cons are quite controversial.) The syntax of the macro is: #define Macro name macro value as a suggestion and a common habit of a large number of programmers, macro names often use all uppercase letters.

Advantages of using Macros: 1) The code is more concise and clear of course, it depends on you to take 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 of the macro processing, in the compilation process called "Preprocessing." 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 partial constants are defined in the format: const data type constant name = constant value, whereas const-defined constants have data types, constants that define data types are 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: the 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. For example, the above code: \ n "); Use the direct input class name to access the static method Counter::getcount//print version of the class, print ("Version useed:". Counter::version. "
\ n ");?>

  • 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.