Application 2019-07-23 static and const keywords

First, to understand at first memory segment, the memory is divided into four segments, stack, heap, code segments, initialize static segment. Different program statement stored in different memory segments, stack space segment where the storage of the data type and length occupy the same space occupied by a small space , for example, integer 1, 10, 100, 1000, 10,000, 100,000, etc., occupy memory space which are of equal length, four bytes are 64 bits. Then the data length of variable length, but also occupies a lot of space on that data type of the memory segment inside it? Such data is placed inside the heap. Stack memory can be directly accessed, and heap memory is not directly accessible memory. For us it is a kind of large object data types and is the type of variable length space, so that the object is on the heap inside , but inside the object name is placed on the stack, so that by the name of the object you can use the object a.

 

static static

From the perspective of our memory to divide the static keyword in the class described member properties and member methods are static; where static member benefits in it? Earlier we declare humanity "Person" of the "Person" If we add the class attribute a "person belongs country", so with the "Person" class to instantiate a few hundred or more instances of an object, each object has properties on the inside "country" is, and if the development of the project was developed for the Chinese people, so each object has a property on which country is the "Chinese" other attributes are different, If we attribute the "State" made-static member, attribute this state only one in memory, and let the hundreds or more of these objects share a property, static members to restrict outside access, because the static members belong to the class, do not belong to any object instance is space in the class for the first time when they were loaded and the distribution of other classes is not accessible, only instance of the class of share, to a certain extent, the members of the class forming a protective ; points analysis moment, memory is logically divided into four sections, where the object is on the "memory stack" inside, was placed in the object reference "stack memory "In, but static members are put on" initialize static segment , "when the class is first loaded into, allowing each object inside the shared memory heap.

class preson {
     public  static  $ Country = "Chinese" ;
     public  static  function say () 
    { 
        echo "I am Chinese, I love China" ; 
    } 
} 

// instantiate a class above 
$ obj = new new preson ();
 echo  $ obj -> Country;

Typically, we are the above methods according to the properties and methods of accessing the class, but the above properties and methods are static, and therefore the system error: Strict Standards: Accessing static property Preson :: $ country as non static.

Because static members is when the class is first loaded on to create, so no external object class and the class name can access the static member; mentioned above, static members are each instance of this class shared objects , then we use the object Can access class static members do? Static members are not present inside each object, but each object can be shared, so if we use the object to access members, they would not have this attribute definition, appear to use object access than static members , in other object-oriented languages, such as Java can use object access static members, if PHP can be used in object access static members, we also try not to use, because the static members of our purpose is to use the class name in doing the project to visit.

Then how can we use the static properties and methods? If you need to use the static properties outside the class, then the class name :: attribute (method) name.

Preson::say();

So that we can get a static property or method you want to use.

 Internal method in a class on how to use the static properties of it? There are two ways ① class name :: property; ②self :: properties. Look at the following code:

class preson {
     public  static  $ Country = "Chinese" ;
     public  static  function say () 
    { 
        echo preson :: $ Country ;
         echo Self :: $ Country ; 
    } 
} 

// access external static method 
Preson :: say ();

The output is: China China. Visible are two ways to access the internal static class property, which is similar to self and $ this, but self is representative of this class where the static methods . So in a static method, the method can be used where this kind of "class name", you can also use the "self" to access other static members, if there are no special circumstances, we generally use the latter, namely " Self member property :: "The way.

Can you access a static member in a non-static method in it, of course, also possible, but can not use "$ this" references, but also the class name, or "self :: member properties of the form."

So? We can access non-static properties do static methods class? The answer is no. Static method in class which can only access static attributes of the class, static methods in the class which is non-static member of the class can not be accessed , the reason is very simple, we want access to other members of the class in this class of methods, we need to use the $ this reference, while $ this object represents the reference pointer is calling this method, we say a static method call is not an object, but rather uses the class name to access, so there is no objects exist, $ this is no reference to this, and there is no reference to this $ this can not access non-static member class inside.

 

 

const constants


const is a key constants defined in the definition of constants in PHP using the "define ()" function, but the definition of the class which is the constant use of "const" keyword, with "const" modified member properties almost access mode and "static" member access modified way, is to use "class name", use "self" keyword inside the method. But do not use the "$" symbol can not be accessed using the object .

class Preson{
    const num = 1000;

    function output(){
        echo Preson::num;
        echo self::num;
    }
}

echo Preson::num;
$obj = new Preson();
$obj->output();

The output 100 010 001 000

The above method can be seen constant access to class.

Guess you like

Origin www.cnblogs.com/zhangxu-fasu/p/11233501.html