PHP abstract classes and abstract methods/static attributes and static methods/Single-profit mode (single-State mode)/serialization and deserialization (serialization and deserialization) /constraint type/magic method summary, serialized serialization

Source: Internet
Author: User

PHP abstract classes and abstract methods/static attributes and static methods/Single-profit mode (single-State mode)/serialization and deserialization (serialization and deserialization) /constraint type/magic method summary, serialized serialization

 

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 ();

 

1 class Singleton {2 private static $ single = null; 3 private function _ construct () {} 4 static function getSingle () {5 if (! Self: $ single) {6 self: $ single = new self (); 7} 8 return self: $ single; 9} 10 function _ destruct () {11 echo "ah, I was destroyed. "; 12} 13} 14 15 $ s1 = Singleton: getSingle (); 16 $ s2 = Singleton: getSingle (); 17 $ s3 = Singleton: getSingle (); 18 $ s4 = Singleton: getSingle ();Simple code in single-Profit Mode

 

 

4. serialization and deserialization (serialization and deserialization)
   
1. serialization: the process of converting an object into a string through a series of operations, called serialization;
2. deserialization: the process of converting serialized strings into objects is called deserialization;
3. When will serialization be used?
① When the object needs to be transmitted over the network;
② Objects must be permanently stored in files or databases;
4. How to Achieve Object serialization and deserialization?
Serializable: $ str = serialize ($ duixiang );
Deserialization: $ duixiang = unserialize ($ str );
5. _ sleep () magic method:
① When the object is serialized, the _ sleep () function is automatically executed;
The _ sleep () function requires that an array be returned. The values in the array are serializable attributes. Attributes not in the array cannot be serialized;
Function _ sleep (){
Return array ("name", "age"); // serializes only the name/age attributes.
}
6. _ wakeup () magic method:
① When the object is deserialized, the _ wakeup () method is automatically called;
② During automatic calling, the new object attributes generated by the deserialization are used for re-copying;
Function _ wakeup (){
$ This-> name = "Li Si ";

 

5 constraint types
   
1. Type constraints: add the data type before the variable to constrain that the variable can only store the corresponding data type. (this operation is common in strong-type languages. In PHP, only the type constraints of arrays and objects can be implemented)
2. If the type constraint is a class, the subclass objects of this class and this class can all be passed;

3. In PHP, type constraints can only occur in function parameters.
Class Person {}
Class Student extend

Function func (Person $ p ){
// Constrain the function parameters. Only the Person class and the Person class are accepted.
Echo & quot; 111 & quot ";
Echo $ p-> name;
}
Func (new Person (); √
Func (new Student (); √
Func ("111"); ×

In the form of new Person ();, we call it an "anonymous function ";

 

6. Summary of magic methods
   
1. _ construct (): constructor. It is automatically called when an object is new.
2. _ destruct (): destructor, which is called automatically before an object is destroyed.
3. _ get (): it is automatically called when the private attribute in the condition class is used. Pass the read attribute name and return $ this-> attribute name
4. _ set (): it is automatically called when values are assigned to private attributes of a class. Pass the attribute name and attribute value to be set
5. _ isset (): it is automatically called when isset () is used to detect the private attributes of an object. Pass the detected attribute name and return isset ($ this-> attribute name );
6. _ unset (): it is automatically called when unset () is used to delete the private attributes of an object. The name of the deleted attribute is passed, and the unset ($ this-> attribute name) is executed in the method );
7. _ toString (): it is automatically called when echo is used to print an object. Returns the actual content when you want to print the object. The returned content must be a string;
8. _ call (): automatically calls an undefined or undisclosed method in a class. Pass the called function name and parameter list array;
9. _ clone (): it is automatically called when an object is cloned using the clone keyword. The function is to initialize and copy the new cloned object;
10. _ sleep (): automatically called when the object is serialized. Returns an array. The values in the array are serialized attributes.
11. _ wakeup (): automatically called when the object is deserialized. Initialize and copy newly generated objects for deserialization;
12. _ autoload (): The function must be declared outside the class. It is automatically called when it is instantiated as a life class. The passed instantiated class name can be used to automatically load the corresponding class file.

 

 

 

Notes may be incorrect during study. Thank you for your criticism.

Reflection, replay, get a little bit every day ------------------- look forward to better yourself

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.