14. Application of the final keyword
This keyword can only be used to define classes and methods. The final keyword cannot be used to define member properties, because
final means constant, we are The define() function is used to define constants in PHP, so final cannot be used to
define member properties.
Classes marked with the final key cannot be inherited;
Code snippet
final class Person{
… …
}
class Student extends Person{
}
Yes The following error occurs:
Fatal error: Class Student may not inherit from final class (Person)
Methods marked with the final key cannot be overridden by subclasses and are the final version;
Code fragment
class Person{
final function say() {
}
}
class Student extends Person{
function say() {
}
}
The following error will occur:
Fatal error: Cannot override final method Person::say()
15. Use of static and const keywords
Static keyword describes member properties and member methods in the class as static; static members What are the benefits?
Earlier we declared the "Person" human being. In the "Person" class, if we add an attribute of "the country to which the person belongs"
, we can use the "Person" class to instantiate hundreds or more There are more instance objects, and each object will
have the attribute of "country to which it belongs." If the project is developed for the Chinese, then each object will
have a country attribute. The attribute is "China" and other attributes are different. If we make the "country" attribute a static
member, then there will be only one country attribute in the memory, and there will be hundreds or more Objects share this attribute.
static members can restrict external access, because static members belong to the class and do not belong to any object instance. They are
space allocated when the class is loaded for the first time. Other classes are inaccessible and are only shared with instances of the class, which can protect the members of the class to a certain extent;
Let’s analyze it from the perspective of memory. The memory is logically divided into four segments. The object is placed in the "heap memory"
, the reference of the object is placed in the "stack memory", and the static members are placed in the "initialized static segment", when the class is
loaded for the first time When placed, it can be shared by every object in the heap memory, as shown below; The static variables of the
code segment
<? class Person{ //下面是人的静态成员属性 public static $myCountry="中国"; // var $name; //人的名子 //这是人的静态成员方法 public static function say(){ echo "我是中国人<br>"; } } //输出静态属性 echo Person::$myCountry; //访问静态方法 Person::say(); //重新给静态属性赋值 Person::$myCountry="美国"; echo Person::$myCountry; ?>
Because static members are created when the class is first loaded, static members can be accessed using the class name without the need for objects outside the class; as mentioned above, static members are used by this class Shared by each instance object, can we use the object to access the static members in the class? From the picture above, we can see that static members do not exist inside each object, but each object can be shared, so if we use an object to access members, there will be no such attribute definition, and the object cannot be accessed. Static members. In other object-oriented languages, such as Java, you can use objects to access static members. If you can use objects to access static members in PHP, we should try not to use them because we use static members. When working on a project, the purpose is to use the class name to access. Static methods in a class can only access the static attributes of the class. Static methods in the class cannot access non-static members of the class. The reason is very simple. We want to access other members of this class in the methods of this class. , we need to use the reference $this, and the reference pointer $this represents the object that calls this method. We said that static methods are not called with objects, but are accessed using names, so there is no object at all, and There is no $this reference. Without the $this reference, non-static members in the class cannot be accessed. And because the static members in the class can be accessed without objects, the static methods in the class can only access the class. Static attributes, since $this does not exist, we use a special class "self" to access other static members in a static method; self is similar to $this, except that self represents the class in which this static method is located. So in a static method, you can use the "class name" of the class where the method is located, or you can use "self" to access other static members. If there are no special circumstances, we usually use the latter, that is, "self:: member" properties" way.
Code snippet
<? class Person{ //下面是人的静态成员属性 public static $myCountry="中国"; //这是人的静态成员方法, 通过self访问其它静态成员 public static function say(){ echo "我是".self::$myCountry."<br>"; } } //访问静态方法 Person::say(); ?>
Can you access static members in non-static methods? Of course it is possible, but you cannot use "$this". References must also use class names or "self:: member attribute form".
const is a keyword for defining constants. To define constants in PHP, you use the "define()" function, but to define constants in a class, you use the "const" keyword, which is similar to that in C. #define If
its value is changed in the program, an error will occur. The member attributes modified with "const" are accessed in the same way as the members modified with "static", and the "class name" is also used. Use the "self" keyword inside the method. But you don't need to use the "$" symbol, and you can't use objects to access it.
Code snippet
<?php class MyClass{ //定义一个常量constant const constant = 'constant value'; function showConstant() { echo self::constant . "\n"; //使用self访问,不要加”$” } } echo MyClass::constant . "\n"; //使用类名来访问,也不加”$” $class = new MyClass(); $class->showConstant(); // echo $class::constant; 是不允许的
For more PHP object-oriented guide and related articles on the use of finalstaticconst keyword, please pay attention to the PHP Chinese website!