This section includes the following technical points: 1. Object-oriented concepts 2. What is the relationship between classes, objects, classes, and objects 3. What is object-oriented programming? 4. How to abstract a class? 5. How to instantiate an object? 6. How to use members in an object? 7. Use special references $ this 8. Constructor and constructor 9. "> <LINKhref =" http:
This section includes the following technical points:
1. Object-oriented concepts
2. What is a class, what is an object, the relationship between a class and an object?
3. What is object-oriented programming?
4. How to abstract a class?
5. How to instantiate an object?
6. How to use members in an object?
7. Special reference to the use of "$ this"
8. Constructor and constructor
9. Encapsulation
10. _ set (), _ get (), _ isset (), and _ unset ()
11. Class inheritance
12. Application of polymorphism
13. Reload the new method
14. Access type
15. Use of static members
16. Application of final keywords
17. _ toString () method
18. Clone object
19. _ call handle call errors
20. Automatically load the class
21. Serialize objects
22. Abstract methods and abstract classes
23. php5 interface technology
1. Object-oriented concepts
Object-oriented Programming (Object Oriented Programming, OOP, object-oriented Programming) is a computer Programming architecture, one basic principle of OOP is that a computer program is composed of a single unit or object that can act as a subroutine. OOP achieves three objectives of Software Engineering: reusability, flexibility, and scalability. To achieve the overall operation, each object can receive, process, and send information to other objects. Object-oriented has always been a hot topic in the field of software development. First, object-oriented is in line with the general rules of human beings. Second, the use of the object-oriented method can make all parts of the system perform their duties and do their best. Open a door for programmers to make their programming code simpler, easier to maintain, and more reusable. Some people say that PHP is not a real object-oriented language. This is a fact. PHP is a hybrid language. You can use OOP or traditional procedural programming. However, for large projects, you may need to use pure OOP in PHP to declare classes and use only objects and classes in your project. I will not talk much about this concept first, because many of my friends are far away from object-oriented programming because they cannot understand it when they come into contact with the object-oriented concept, so they don't want to learn it anymore. After reading the entire content, you can understand the concept.
2. What is a class, what is an object, the relationship between a class and an object?
Class Concept: A class is a set of objects with the same attributes and services. It provides a unified abstract description for all objects belonging to this class, which includes two main parts: attribute and service. In an object-oriented programming language, a class is an independent program unit. It should have a class name and contain two main parts: attribute description and service description.
Object: an object is an entity used to describe objective things in a system. It is a basic unit of a system. An object consists of a group of attributes and a group of services that operate on these attributes. From a more abstract point of view, an object is an abstraction of some things in the problem domain or implementation domain. It reflects the information that the things need to be stored in the system and its role; it is the encapsulation body of a set of attributes and a group of services that have the right to operate on these attributes. The objective world is composed of links between objects.
The relationship between a class and an object is like the relationship between a mold and a casting. The instantiation result of a class is an object, and the abstraction of a class object is a class. Class describes a group of objects with the same features (attributes) and the same behavior (methods.
The above is probably their definition. Maybe you are new to object-oriented friends and should not be confused by concepts. Here is an example for you, if you want to buy several assembled PCs in Zhongguancun, what do you do when you get there? Are the installation engineers sitting with you, based on the information you provide, complete an installation configuration sheet with you. This configuration sheet can be considered a class, and it is a piece of paper, however, it records the information of the PC you want to buy. If you use this configuration item to buy 10 machines, the 10 machines are all composed of the configuration items, therefore, these 10 machines are of the same type or category. So what is an object? The instantiation result of the class is an object. The machine that is configured (instantiated) with this configuration sheet is an object, which is an object that we can operate on. There are 10 machines and sub-databases, 10 objects. Each sub-machine is independent and can only indicate that they are in the same category. Any action on one of the sub-machines will not affect the other nine machines, but I modify the class, that is to say, if one or fewer accessories are added to the configuration list, the Nine hosts that have been installed are changed. This is the relationship between classes and objects (the instantiation result of classes is objects ).
3. What is object-oriented programming?
If you want to build a computer classroom, you must first have a room with N computers, N tables, N chairs, and whiteboards, projector, etc. What are these? We just talked about it. This is an object. We can see entities. We can say that the units in this computer classroom are physical objects, they make up the computer classroom together. So what is the relationship between our program and the object-oriented system? Developing a system program is similar to building a computer classroom. You Abstract each independent function module into a class to form an object, which consists of multiple objects, these objects can receive information, process data, and send information to other objects. It constitutes an object-oriented program.
4. How to abstract a class?
As mentioned above, the unit of the object-oriented program is the object, but the object is instantiated through the class, so the first thing we need to do is how to declare the class, it is easy to create a class. You only need to master the basic program syntax and definition rules. Where is the difficulty? How many classes are used in a project, how many objects are used, where classes are to be defined, what kind of classes are defined, how many objects are produced by this class instance, and how many attributes are contained in the class, how many methods are there, etc. This requires the reader to analyze and design the actual problems in actual development.
Class definition:
Class name {
}
Use a keyword class and a class name you want and a pair of braces. The structure of this class is defined. You only need to write code in it, but what is written in it? What can I write? How to write is a complete class? As mentioned above, the class is used to let it instance out of objects for us to use, so we need to know what kind of object you want, as we mentioned above, what is written on an installation configuration sheet is what you have installed on the machine. For example
Is an object. How do you recommend a person you are optimistic about to your leaders? Of course, the more detailed the better:
First, you will introduce the person's name, gender, age, height, weight, phone number, home address, and so on.
Then, you will introduce what this person can do. He can drive, speak English, and use a computer.
As long as you have a little introduction, others will have a better understanding of this person. This is our description of a person.
We can describe all objects using classes in a similar way. From the description above, we can see that a class is made, which is divided into two parts from the definition point of view, the first is static description, the second is dynamic description, and the static description is what we call
Attributes, as we can see above, the person's name, gender, age, height, weight, phone number, home address, and so on. It is also the function of the person's object on the fly. For example, when a person can drive, speak English, or use a computer, we can write dynamic functions or methods into a program, functions and methods are the same. Therefore, all classes are written in terms of attributes and methods. Attributes are also called member attributes of this class, and methods are called member methods of this class.
Class {
Member attributes: name, gender, age, height, weight, phone number, home address
Member method: can drive, can speak English, can use a computer
}
Attribute:
The variable is declared by using the keyword "var" in the class definition, that is, the class attribute is created. Although the initial value can be given when the member attribute is declared, however, when declaring a class, it is unnecessary to give the initial value of the member attribute. For example, if the name of a person is assigned "zhang san", dozens of people will be generated using this class instance, these dozens of people are called James, so there is no need to. After the instance outputs an object, we can give the initial value of the member attribute.
For example, var $ somevar;
Method (member function ):
The class method is created by declaring a function in the class definition.
For example, function somefun (parameter list)
{......}
Class Person
{
// The following are the member attributes of a person.
Var $ name; // name of a person
Var $ sex; // gender of a person
Var $ age; // age of a person
// The following is the member method of a person.
Function say () // The way this person can speak
{
Echo "this person is talking ";
}
Function run () // this person can walk
{
Echo "this person is walking ";
}
}
?>
The above is the declaration of a class, a class declared from the attributes and methods, but it is best to declare the member attributes
Do not give the initial value, because the class we are working on is a descriptive information and will be used to instantiate objects. For example, if 10 objects are instantiated, each person's name, gender, and age are different. Therefore, it is best not to assign an initial value to the member attribute in this place, but to assign values to each object separately.
You can use the same method to create the desired class. As long as you can define the object as a class with the attributes and methods, you can instantiate the object.
To enhance your understanding of the class, let's create a class with a wide range of shapes. Let's make a rectangle and analyze it first, what are the attributes of a rectangle? What are the functions of rectangles?
Class rectangle
{
// Attributes of the rectangle
The length of the rectangle;
The width of the rectangle;
// Rectangle method
The perimeter of the rectangle;
The area of the rectangle;
}
Class Rect
{
Var $ kuan;
Var $ gao;
Function zhouChang ()
{
Calculates the perimeter of a rectangle;
}
Function mianJi ()
{
Calculates the area of the rectangle;
}
}
?>
If you use this class to create multiple rectangle objects, each rectangle object has its own length and width, you can find its own length and area.