The static keyword is described in a class where member properties and member methods are static, and where are the benefits of static members? Before we declare the "person" of the human beings, in the class "people" if we add a "human state" attribute, so that the class "man" to instantiate hundreds of or more instance objects, each object has a "state" property, If the development of the project is developed for the Chinese, then each object has a national property is the "China" other properties are different, if we make the "state" of the properties of static members, so that the country's properties in memory only one, and let these hundreds of or more objects share this property, Static members can restrict external access because static members belong to the class, are not part of any object instance, are allocated space when the class is loaded for the first time, other classes are inaccessible, only instances of the class are shared, and the members of the class can be protected to a certain extent;
From the memory point of view, we will analyze, the memory is logically divided into four segments, where the object is placed in "heap memory", the object's reference is placed in the "stack Memory", and the static member is placed in the "initialization static segment", in the first time the class is loaded, you can let each object in the heap memory is shared, such as:
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.
05 |
public static $myCountry = "中国" ; |
10 |
public static function say() { |
16 |
echo Person:: $myCountry ; |
22 |
Person:: $myCountry = "美国" ; |
23 |
echo Person:: $myCountry ; |
Because static members are created at the time the class is first loaded, it is possible to use the class name to access static members that do not require an object outside of the class, as stated above, when a static member is shared by each instance object of the class, can we use the object to access the static members of the class? As we can see, the static members are not present within each object, but each object can be shared, so if we use objects to access the members, there will be no such attribute definition, the use of object access to static members , in other object-oriented languages, For example, Java can be used to access static members of the object, if you can use the object in PHP to access static members, we also try not to use, because the static members when we do the project is to use the class name to access.
Static methods inside a class can only access static properties of the class, and static methods within the class cannot access the non-static members of the class, for the simple reason that we want to access the other members of this class in a method of this class, we need to use the $this reference, and $ This reference pointer is the object that calls this method, we say that the static method is not called by the object, but using the class name to access , so there is no object exists, there is no $this this reference, no This reference cannot access the non-static members of the class, and because the static members of the class can be accessed without objects, the static method inside the class can only access the static properties of the class, that is, $this does not exist, and in static methods we use a special class " self "; Self is similar to $this, except that self is the class that represents this static method. In a static method, you can use the class name of the class in which the method resides, or you canuse "self" to access other static members, and if there is no special case, we usually use the latter, the "Self:: member Property ".
05 |
public static $myCountry = "中国" ; |
07 |
//这是人的静态成员方法, 通过self访问其它静态成员 |
08 |
public static function say() { |
09 |
echo "我是" . self:: $myCountry ; |
It is also possible to access static members in non-static methods, but you cannot use the "$this" reference, or use the class name or "self: the form of a member property."
Const is a keyword that defines constants, and in PHP defines constants using the "define ()" function, but defining constants in a class uses the keyword "const", similar to the C # # If you change its value in a program, an error occurs with the " The const "Decorated member property is accessed in much the same way as the members of the" static "adornment, using the" self "keyword in the method using the" Class name ". However, you do not have to use the "$" symbol and you cannot use objects to access it.
05 |
const constant = 'constant value' ; |
07 |
function showConstant() { |
08 |
echo self::constant . " " ; // 使用self访问,不要加“$” |
12 |
echo MyClass::constant . " " ; //使用类名来访问,也不加“$” |
15 |
$class ->showConstant(); |
16 |
// echo $class::constant; 是不允许的 |
The above describes the static and const PHP, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.