This article will introduce you to some usage details of php object-oriented methods. here we will mainly talk about classes and object learning notes in php, and hope to help you. Object-Oriented Thinking
This article will introduce you to some usage details of php object-oriented methods. here we will mainly talk about classes and object learning notes in php, and hope to help you.
Object-Oriented Thinking
Object-Oriented Programming (OOP) is a type of Programming model, and colleagues are also a method of Programming. It uses objects as the basic unit of a program and encapsulates programs and data to improve software reusability, flexibility, and scalability.
Process-oriented, object-oriented, and functional programming are known as the three major paradigms in programming languages (in fact, process-oriented and object-oriented are similar to imperative programming). They are three different coding and design styles. The core idea of object-oriented is Objects, encapsulation, reusability and scalability.
Object-oriented is a more advanced and abstract way of thinking. although process-oriented is also an abstraction, process-oriented is a basic abstraction, object-oriented is a high-level abstraction built on process-oriented and above. Therefore, object-oriented understanding is not that easy.
Class is the description of a group of objects in our team.
In php, the definition of each class starts with the keyword class, followed by the class name, followed by a pair of curly braces, containing the definition of class members and methods, as shown in the following code:
- Class person {
- Public $ name;
- Public $ gender;
- Public function say (){
- Echo $ this-> name. "is". $ this-> gender;
- }
- }
The following code generates an instance of this class:
- $ Student = new person ();
- $ Student-> name = "Tome ";
- $ Student-> gender = "male ";
- $ Student-> say ();
- $ Teacher = new person ();
- $ Teacher-> name = "kati ";
- $ Teacher-> gender = "female ";
- $ Teacher-> say ();
This code instantiates the person class and generates an instance of the student object and teacher object. In fact, it is the process from abstraction to concrete.
Some understandings of classes and objects:
Class defines a series of attributes and methods, and provides actual operation details. these methods can be used to process attributes.
The object contains the specific value of the class property, which is the class instantiation. It is precisely because of different attributes that different objects can be distinguished. In the preceding example, student and teacher are separated by gender and name.
The relationship between classes and objects is similar to the relationship between services, processing, and processing. Specifically, it is like the relationship between raw materials and pipelines, you only need to call methods in the class on the object to process the class attributes and display their functions.
Print the student object. the code is as follows:
- Print_r (array) $ student );
- Var_dump ($ student );
Serialization object. the instance code is as follows:
- $ Str = serialize ($ student );
- Echo $ str;
- File_put_contents('store.txt ', $ str );
- // Output result: 0: 6: "person": 2: {s: 4: "name"; s: 3: "Tom"; s: 6: "gender "; s: 4: "mail ";}
Deserialization object. the instance code is as follows:
- $ Str = file_get_contents('store.txt ');
- $ Student = unserialize ($ str );
- $ Student-> say ();