Analysis of the use of static,const and define in PHP _php skills

Source: Internet
Author: User
Tags define null
Define section:
Macros can be used not only in place of constant values, but also in substitution of expressions or even code snippets. (Macros are powerful, but they are also prone to errors, so the pros and cons are quite controversial.) )
The syntax for macros is:
#define Macro Name Macro value
As a suggestion and a common habit of programmers, macro names often use all uppercase letters.
Advantages of using macros:
1 To make the code more concise and clear
Of course, it's up to you to take a proper name for the macro. In general, the name of the macro should pay more attention to have a clear intuitive meaning, sometimes rather let it longer.
2 Convenient Code maintenance
The processing of macros, referred to as "preprocessing" during compilation. In other words, the compiler must first replace the macro that appears in the code with its corresponding macro value before it is formally compiled, a process that is a bit of a look-and-feel replacement in word processing software. So using the macro expression constants in your code ultimately uses the immediate number and does not explicitly specify the type of this amount.

Const part
The format of the constants definition is:
Const data type constant name = constant value;
Const-defined constants have data types, and constants that define data types make it easier for the compiler to check for data and cause errors to be checked by the program. Constants must first specify a value, 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 for macro definition, will not allocate memory space, the compilation will be in the main function in the replacement, but simply replace, will not do any checks, such as type, sentence structure, that is, macro-defined constants is simply 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 (there are questions please find the C language book to see the preprocessing section or see MSDN.) const-defined constants have data types, and constants that define data types make it easier for the compiler to check for data, making it possible for the program to troubleshoot. So the difference between a const and a define is that the const definition constant excludes the security between programs. Define defines global constants that can be accessed anywhere

Const is used for class member variable definition, can only be accessed with class name cannot be changed if the beginner is so understanding, don't be too dead-minded on the line PHP5 has added many object-oriented ideas, PHP5 object-oriented approach to Java object-oriented thinking. Here we have a description of the static and const keyword functions in PHP5, and hope to be of help to learn PHP5 's friends.

(1) The Staticstatic keyword is in the class, describes a member is static, static can restrict external access, because the static member belongs to the class, is not belong to any object instances, other classes are inaccessible, only to the class instance share, Can be a certain procedure to protect the members. A static variable of a class, very similar to a global variable, can be shared by instances of all classes, and the static method of the class is the same, similar to the global function. Static methods of a class can access the static properties of a class. Additionally, static members must be accessed using self, and using this will make an error.

(2) Constconst is a keyword that defines constants, similar to the #define in C, that can define a constant, and if it changes its value in the program, an error occurs. Illustrate the code above:
Copy Code code as follows:

<?php
Class Counter
{
private static $count = 0;//defines a static property
Const VERSION = 2.0;//defines a constant
Constructors
function __construct ()
{
Self:: $count + +;
}
destructor
function __destruct ()
{
Self:: $count-;
}
To define a static method
static function GetCount ()
{
Return self:: $count;
}
}
Create an instance
$c = new Counter ();
Performing printing
Print (Counter::getcount (). "<br>\n"); Use the direct input class name to access the static method Counter::getcount
Print a version of a class
Print ("Version useed:".) Counter::version. "<br>\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.