Application of 14.final Keywords
This keyword can only be used to define classes and define methods, and you cannot use the final keyword to define member properties because
For final is the meaning of constants, we define constants in PHP using the Define () function, so we cannot use final to
Define member properties.
Classes that use the final key tag cannot be inherited;
Code fragment
Final class person{
... ...
}
Class Student extends person{
}
The following error will appear:
Fatal Error:class Student may isn't inherit from final Class (person)
The final key tag method cannot be overridden by a quilt, and is the ultimate version;
Code fragment
Class person{
Final function say () {
}
}
Class Student extends person{
function say () {
}
}
The following error will appear:
Fatal Error:cannot Override final Method Person::say ()
Use of the 15.static and const keywords
The static keyword describes the member property and the member method as static in the class, and the static member benefits are there?
We have previously declared human beings, in the category of "person" if we add a "human state"
Property so that hundreds of or more instance objects are instantiated with the class "person", and each object is
There are "country" attributes, if the development of the project is developed for the Chinese, then each object inside will be
One of the attributes of a country is "China" other attributes are different if we put the "state" attribute into a static
Members, so that the attributes of the state in memory are only one, and let these hundreds of or more objects share this attribute,
A static member can restrict external access because the static member belongs to the class, is not part of any object instance, is
The space that is allocated when the class is first loaded, the other classes are inaccessible, shared only with instances of the class, and can be
degree to the class of the member form protection;
From the memory point of view, we analyze that the memory is logically divided into four segments, where the object is placed in "heap memory"
, the object's reference is placed in "stack memory," while the static member is placed in the "Initialize static segment", the first time the class
When it is loaded, it can be shared by each object in the heap memory, as shown below;
' This.width= '; if (this.height> ') ' this.height= '; border=0>
A static variable of a class, very similar to a global variable, can be shared by instances of all classes, as is the static method of a class
, similar to global functions.
Code fragment
Copy Code code as follows:
?
Class person{
The following is a static member property of a person
public static $myCountry = "China";
var $name; The name of a man
This is a static member of the human method
public static function say () {
echo "I am a Chinese <br>";
}
}
Output static properties
echo Person:: $myCountry;
Accessing static methods
Person::say ();
Assign a value to a static property again
Person:: $myCountry = "United States";
echo Person:: $myCountry;
?>
Because static members are created when the class is first loaded, the class is used outside of the class without the need for an object
Name can be accessed to a static member; it says that static members are shared by each instance object of the class.
Can we use objects to access static members in a class? From the image above we can see that the static members are not in the
Each object exists internally, but each object can be shared, so if we use an object to access a member, we will
There is no definition of this attribute that uses objects to access static members in other object-oriented languages, such as
Java is a way to access static members using objects, if you can use objects to access static members in PHP,
We also try not to use it, because the static members we are doing the project when the purpose is to use the class name to access.
Static methods within a class can only access static properties of a class, and static methods inside a class cannot access the Non-static
The reason is very simple, we want to access the other members of this class in the method of this class, we need to use the $this
This reference, while $this this reference pointer is the object that calls this method, we say the static method is not to object
Called, but is accessed using the class name, so there is no object at all, there is no $this this reference, no
$this This reference will not be able to access the non-static members of the class, and because the static members of the class can be used without the object
To access, so the static method inside the class can only access the static properties of the class, that is, the $this does not exist in the static method
Other static members in the interview we are using a special class "self"; self is similar to $this, but self is representative
The class in which this static method resides. So in a static method, you can use the class name of the class in which the method is located, or you can
Using "self" to access other static members, if there is no special case, we usually use the latter, namely "Self::
The way of the member property ".
Code fragment
Copy Code code as follows:
?
Class person{
The following is a static member property of a person
public static $myCountry = "China";
This is the static member method of the person that accesses other static members through self
public static function say () {
echo "I am". Self:: $myCountry. " <br> ";
}
}
Accessing static methods
Person::say ();
?>
It is also possible to access static members in Non-static methods, but you cannot use the $this
References also use the class name or the self:: Member property form.
Const is a keyword that defines constants, which are defined in PHP using the "define ()" function, but
is to define constants in the class using the keyword "const", similar to the #define in C if you change in the program
Its value, an error occurs, the access of the member property that is decorated with "const", and the members modified by "Static"
Access is similar, using "Class name" and using the "self" keyword in the method. But don't use "$"
symbols, nor can you use objects to access them.
Code fragment
Copy Code code as follows:
<?php
class myclass{
//define a constant constant
Const constant = ' Co Nstant value ';
Function Showconstant () {
echo self::constant. "\ n"; Use self to access, do not add "$"
}
Echo myclass::constant. "\ n"; Use the class name to access, and not add "$"
$class = new MyClass ();
$class->showconstant ();
//Echo $class:: constant; is not allowed