This keyword can only be used to define classes and define methods, and cannot use the final keyword to define member properties, because final is the meaning of constants, we define constants in PHP using the Define () function, so you cannot use final to define member properties.
Application of 14.final Keywords
This keyword can only be used to define classes and define methods, and it is not possible to 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 you cannot use final to
Define member properties.
Classes that use the final key tag cannot be inherited;
Code Snippets
Final class person{
... ...
}
Class Student extends person{
}
The following error will appear:
Fatal Error:class Student may not inherit from final Class (person)
The method of using the final key tag cannot be overridden by the quilt class, which is the final version;
Code Snippets
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 15.static and const keywords
The static keyword is described in a class where member properties and member methods are static; Where are the benefits of static membership?
In front of us, we declared "person" human beings, in the category "People" if we add a "human state"
attribute, so that hundreds of or more instance objects are instantiated with the class "person", each of which
There is a "country" attribute, if the development of the project is developed for the Chinese, then each object inside is
There is a property of a country that is "China" other attributes are different if we make the "country" attribute static
Member, so that the properties of the country are only one in memory, and let the 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 instances, and are
When the class is loaded for the first time, the space allocated, other classes are inaccessible, only the instance of the class is shared, can be a certain process
Protection of the members of the class;
From the memory point of view, we analyze that the memory is logically divided into four segments, where objects are placed in "heap memory"
, the object's reference is placed in the "stack Memory", and the static member is placed in the "Initialize static segment", the first time the class
When loaded, it can be shared by each object in the heap memory, such as;
The static variables of a class, very similar to global variables, can be shared by instances of all classes, as well as static methods of classes
, similar to a global function.
Code Snippets
Copy the 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 the man
This is the static member method of the person
public static function say () {
echo "I am a Chinese
";
}
}
Output static properties
echo Person:: $myCountry;
Accessing static methods
Person::say ();
Re-assigning a value to a static property
Person:: $myCountry = "United States";
echo Person:: $myCountry;
?>
Because static members are created the first time a class is loaded, you do not need the object outside of the class to use the class
The name can be accessed to a static member; As stated above, a static member is shared by each instance object of the class.
Can we use objects to access static members in a class? From which we can see that the static members are not in
Each object exists inside, but each object can be shared, so if we use the object to access the member, we will
There is no such attribute definition, the use of object access to static members, in other object-oriented languages, such as
Java is a way to access static members using objects, and if you can use objects to access static members in PHP,
We also try not to use it because static members are used to access the class name when we are doing the project.
Static methods inside a class can only access static properties of the class, and static methods inside the class are not accessible to the class's non-static
For the reason that we want to access the other members of this class in our methods, we need to use the $this
This reference, while $this this reference pointer is the object that represents the call to this method, we say that the static method is not the object
Call, but use the class name to access, so there is no object exists, there is no $this this reference, no
$this This reference cannot access non-static members of the class, and because static members inside the class can be used without objects
To access, so the static method inside the class can only access the static properties of the class, that is, $this does not exist, in the static method
Visit other static members we are using a special class "Self", and self is similar to $this, except that self is the representative
The class in which this static method resides. In a static method, you can use the class name of the class where the method resides, or you can
Use "Self" to access other static members, if there is no special case, we usually use the latter, namely "Self:: Into
The method of the member attribute.
Code Snippets
Copy the 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 a person, accessing other static members through self
public static function say () {
echo "I am." Self:: $myCountry. "
";
}
}
Accessing static methods
Person::say ();
?>
It is also possible to access static members in non-static methods, but you cannot use "$this"
The reference also uses the class name or "Self:: The form of the member property."
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 C # # If you change in the program
The value, then an error occurs, and the member property is accessed with the "const" modifier and the members of the "static" modifier
Access is similar to the use of "class name", in the method using the "Self" keyword. But do not use "$"
Symbols, and cannot be accessed using objects.
Code Snippets
Copy the Code code as follows:
Class myclass{
Defines a constant constant
CONST CONSTANT = ' constant 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; It's not allowed.
The above describes the use of the PHP object-oriented final static const keyword, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.