Stack heap static initialization code segment segment

 

Our existence is instantiated object heap memory, stack memory when there is instantiated objects out

 

static keyword in the class described member properties and member methods are static

static members to restrict access to the outside, since the members belong to the class of static, 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 shared to some extent to form a protective member of the class ;

From the perspective of our memory to analyze, memory is divided into four sections logically, the object of which is on the "heap" which references the object was placed in the "stack memory" in, but static members are put on " initialization static segment " , when the class is first loaded into, allowing each object inside the heap memory is shared, as shown below:

 

 

? <
 Class the Person {
     // Here is a static member who attributes 
    public  static $ MyCountry = " Chinese " ; 
 
    // var $ name; // person's name 
 
    // This is a human static member method 
    public  static function say () { 
        echo " I am Chinese " ; 
    } 
} 
 
// output static properties 
echo the person :: $ MyCountry; 
 
// access static methods 
the person :: say (); 
 
// re-assigned to the static properties of 
the person :: $ MyCountry = " US " ; 
echo the Person :: $ MyCountry;
 ?>

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$ this this reference, while $ this object represents the reference pointer is calling this method, we say a static method call is not an object, but the use of the class name to access , so there is no objects exist, there is no$ this the references, there is no $ this reference to the non-static class members can not access the inside, but also because the class inside a static member can not object to access, so a static method of the class can only access the class static properties, now that there is no $ this, visit other static members in a static method we use is a special class of "self"; self and $ this is similar,but theself 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.

 

? < PHP
 class the Person {
     // Here is a static member who attributes 
    public  static $ MyCountry = " Chinese " ; 
 
    // this is a static member Method Man, and access other static members through Self 
    public  static function say () { 
        echo " I is " . Self :: $ MyCountry; 
    } 
} 
 
// access static methods 
the Person :: say ();
 ?>

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

 Brightening is defined const keyword, when lit defined in php "DEFINE ()" function, but in which the definition of the class Always use "const0 keyword"

With access method modified member properties "const" and "static" almost a modified form, member access, as well as the use of "class name"

Inside the method used in the "self" keyword. But do not use the "$" symbol can not be accessed using the object .

? < PHP
 class MyClass {
     // define a constant Constant 
    const Constant = ' Constant value ' ; 
 
    function showConstant () { 
        echo self :: Constant. "  " ; // use self access, do not add "$" 
    } 
} 
 
echo MyClass: .: Constant "  " ; // use the class name to access, do not add "$" 
 
$ class = new new MyClass (); 
$ class -> showConstant ();
 // echo $ class :: Constant; // is not allowed of 
?>

 

Guess you like

Origin www.cnblogs.com/Aleen/p/11233964.html