Previously, OOP has been studying PHP for a long time. today I will summarize the static attributes of abstract classes and abstract methods in PHP and the single-profit mode (single-state mode) in PHP) serialization and deserialization (serialization and deserialization ). 1. abstract classes and abstract methods in PHP
Preface
OOP
I have been studying PHP for a long time. today I will summarize the abstract classes and abstract methods in PHP, static attributes, and static methods, and the single-profit mode (single-state mode) in PHP) /serialization and deserialization (serialization and deserialization ).
1. abstract classes and abstract methods in PHP
|
1. what is an abstract method?
The method body {} does not exist. you must use the abstract keyword to modify the method body. This is called an abstract method.
Abstract function say (); // abstract method
2. what is an abstract class?
Classes that contain abstract methods are called abstract classes. Abstract classes must be modified using abstract keywords.
Abstract class Person {}
3. notes for abstract classes
① Abstract classes can contain non-abstract methods;
② Classes that contain abstract methods must be abstract classes;
Abstract class, not necessarily including abstract methods;
③ Abstract class, which cannot be instantiated.
(Abstract classes may contain abstract methods. abstract methods do not have method bodies, and instantiation calls do not make sense .)
The purpose of using abstract classes! Restrict instantiation !!!
4. if the subclass inherits the abstract class, the subclass must override all abstract methods of the parent class. Unless the subclass is also an abstract class.
5. what is the role of an abstract class?
① Restrict instantiation. (An abstract class is an incomplete class. the abstract method in it does not have a method body, so it cannot be instantiated)
② Abstract classes provide a standard for subclass inheritance. If a subclass inherits an abstract class, it must contain and implement the defined abstract methods in the abstract class.
2. static attributes and static methods
|
1. static
① Attributes and methods can be modified. they are called static attributes and static methods, also called class attributes and class methods;
② Static attributes and static methods can only be called directly using class names.
Use "class name: $ static property", "class name: static method ()"
Person: $ sex; Person: say ();
③ Static attributes and methods are declared during class loading. Generated before the object;
④ Non-static attributes or methods cannot be called in static methods;
Non-static method. you can call static attributes or methods;
(Because static attributes and methods have already been generated during class loading, instead of static attribute methods, they have not yet been instantiated yet)
⑤ In the class, you can use the self keyword to represent the class name.
Class Person {
Static $ sex = "nan ";
Function say (){
Echo self: $ sex;
}
}
⑥ Static attributes are shared. That is, many new objects share a property.
2. final
① Final modifier class, which is the final class and cannot be inherited;
② Final modifier. this method is the final method and cannot be overwritten!
③ Final cannot modify attributes.
3. const keywords;
Declare constants in the class. The define () function cannot be used! You must use the const keyword.
Similar to the define () declaration, the const keyword declaration constant cannot contain $, and must all be capitalized!
Once declared, the constant cannot be changed. The call time is the same as static, and the class name Emperor loves to use Person: constant.
[Summary] several special operators
1. only strings can be connected ;"".""
2. => declares that the array is. the join key and value ["key" => "value"]
3.-> The $ this new object is low. use the member attributes and member methods.
4.: ① use the parent keyword to call the method with the same name in the parent class; parent: say ();
② Use the class name (and self) to call static attributes, static methods, and constants in the class.
3. single-profit mode in PHP (single-state mode)
|
The single-profit mode is also called the single-state mode.
It can be guaranteed that only one object instance can exist in a class.
Implementation points:
① The constructor is private and cannot use the new keyword to create an object.
② Provide methods for obtaining objects externally. In the method, determine whether the object is null. if it is null, create an object and return it. if it is not null, put it back directly,
③ Attributes of the instance object and methods of the past object must be static.
④ Then, the object can only be created using the static method we provide. $ S1 = Singleton: getSingle ();
Notes may be incorrect during study. thank you for your criticism.
Reflection, replay, get a little bit every day ------------------- look forward to better yourself
The above is a detailed description of some common methods in php. For more information, see other related articles in the first PHP community!