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.
<?class person{//The following is a static member property of a person public static $myCountry = "China";//var $name;//person's name//This is a static member method of human public static function SA Y () {echo "I am Chinese";}} Output static property echo Person:: $myCountry;//Access static method Person::say ();//re-assign a value to a static property person:: $myCountry = "United States"; 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 ".
<?class person{//The following is a static member property of a person public static $myCountry = "China";//This is a static member method of a person that accesses other static members public static function say () through self {echo "I am". Self:: $myCountry;}} Access static method Person::say ();? >
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 # in C Define if the value is changed in the program, an error occurs, and the member property that is decorated with "const" is accessed in much the same way as the members of the "static" modifier, and the "self" keyword is used in the method with the "class name". However, you do not have to use the "$" symbol and you cannot use objects to access it.
<?phpclass myclass{//defines a constant constantconst constant = ' constant value '; function Showconstant () {echo self::constant. " "; Use self to access, do not add "$"}}echo myclass::constant. " "; Use the class name to access, without adding "$" $class = new MyClass (); $class->showconstant ();//echo $class:: constant; It's not allowed,?>.
"Getting Started with PHP object-oriented (OOP) programming" 15.static and the use of the Const keyword (self::)