Const, define, and static in php5

Source: Internet
Author: User
Tags define definition
In php5, const, define, and static 1. in const programming, we generally use constants to define the constant values that cannot be changed during runtime. let's take a look at const in php, there is nothing to say about this, just to improve the knowledge. When defining a constant, we can use const to modify the constant. the constant modified with const is a bit different from other constants: const, define, and static in php5 before the constant name.
1. const
In programming, we generally use constants to define the constant values that cannot be changed during runtime. let's take a look at the const in php. there is nothing to say about this, let's just talk about it for the sake of knowledge perfection. When defining a constant, we can use const to modify the constant. the constant modified with const is a bit different from other constants. Instead, we should not use "$" before the constant name ", remember! Of course, this constant value cannot be modified. once defined, it cannot be modified "manually" anywhere in the program. This is the same as the define definition, and the use of const to define it also complies with the naming rules of other constants-use uppercase letters.

Let's look at a small example:
Class say_const {
Const CHARSET = "China ";
Public function say_hello (){
Echo slef: CHARSET;
}
}
$ Const1 = new say_const ();
$ Const1-> say_hello ();

For more information, see the following extension:
There is no dollar sign before the constant ($ );
Constants can only be defined using the define () function, but cannot be defined using the value assignment statement;
Constants can be defined and accessed anywhere, regardless of the variable range rules;
Once defined, a constant cannot be redefined or canceled;
The constant value can only be a scalar;
Constants can only contain scalar data (boolean, integer, float, and string). do not define resource constants.
You can use the constant () function to read the value of a constant. get_defined_constants () to obtain a list of all defined constants.
If an undefined CONSTANT is used, PHP assumes that it wants the name of the CONSTANT, just like calling it with a string (CONSTANT corresponds to "CONSTANT "), an E_NOTICE error is reported.


PHP's "Magic constant ".
_ LINE __
_ FILE __
_ FUNCTION __
_ CLASS __
_ METHOD __


In object-oriented
Interface:
1. the static variables cannot be defined in the class member variables (except constants)
2. the defined constant const YOUCONST = VALUE cannot be overwritten in the subclass. it is called in interfaceName: YOUCONST method in the subclass.
3. you cannot use the parent: YOUCONST method to call the constant of an interface.

Class:
1 The const variable can be accessed in the subclass using the parent: YOUCONST method and className: YOUCONST method.
2. the variable defined by const can be overwritten in the subclass.
3. of course, static member variables can be defined in the class.


II. define
What is a custom constant?

A custom constant represents another object with a character. this object can be a value, a string, a Boolean value, and so on. Its definition has many similarities with variables. There is only one difference, that is, the variable value can be changed at will during the program running, and once the custom constant is defined, it can no longer be modified in the program running.
The definition is as follows:
Define ("YEAR", "2012 ");
Use the define keyword to bind the 2012 string to YEAR. in the future, 2012 will be used in place of YEAR in the program. In general, when we define constants, the constant names use uppercase letters. For example,
Define ("YEAR", "2012 ");
Define ("MONTH", "12 ");
Define ("DATE", "21 ");
Define ("THING", "Doomsday ");
Echo YEAR. "-". MONTH. "-". DATE. "". THING;
?>



III. static
The static keyword is in the class, describing a member is static, and static can restrict external access, because the static member belongs to the class and does not belong to any object instance, other classes cannot be accessed. they are 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.


The difference between a static variable and a local variable is that when a static variable leaves its scope, its value will not automatically disappear, but will continue to exist. when it is used again next time, the last value can be retained.
For example:
    
 \ N "); // enter the class name directly 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.