Home >Backend Development >PHP Tutorial >Characteristics of object-oriented PHP object-oriented guide (1) Basic knowledge of object-oriented
1. The concept of object-oriented programming
Object-oriented programming (OOP, object-oriented programming) is a computer programming architecture. A basic principle of OOP is that a computer program is composed of a single unit or object that can function as a subroutine Combined, OOP achieves the three goals of software engineering: reusability, flexibility, and extensibility. In order to realize the overall operation, each object can receive information, process data and send information to other objects. Object-oriented has always been a hot topic in the field of software development. First of all, object-oriented is in line with the general rules of how humans look at things. Secondly, the use of object-oriented methods allows each part of the system to perform its duties and perform its duties. This opens the door for programmers to write code that is simpler, easier to maintain, and more reusable. Some people say that PHP is not a true object-oriented language,
This is true. PHP is a hybrid language, you can use OOP or traditional procedural programming. However, for large projects, you may need to use pure OOP to declare classes in PHP, and only use objects and classes in your project. I won’t go into detail about this concept, because the main reason why many friends stay away from object-oriented programming is that they can’t understand the object-oriented concept when they come into contact with it, so they don’t want to learn it. Let the readers understand the concept after reading the entire content.
2. What is a class, what is an object, and the relationship between classes and objects
Concept of class: A class is a collection 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: properties and services. In object-oriented programming languages, a class is an independent program unit. It should have a class name and include two main parts: attribute description and service description.
The concept of object: An object is an entity used to describe objective things in the system. It is a basic unit that constitutes the system. An object consists of a set of properties and a set of services that operate on the set of properties. From a more abstract perspective, an object is an abstraction of something in the problem domain or implementation domain. It reflects the information that needs to be saved and the role that the thing plays in the system; it is a set of attributes and rights. These properties encapsulate a set of services that operate on them. The objective world is composed of objects
and the connections between objects.
The relationship between classes and objects is like the relationship between molds and castings. The instantiation result of a class is an object, and the abstraction of a type of object is a class. A class describes a group of objects that have the same characteristics (properties) and the same behavior (methods).
The above is probably their definition. Maybe you are new to object-oriented friends. Don’t be confused by the concepts. Let me give you an example. If you go to Zhongguancun and want to buy a few assembled PCs, when you get there What is your first step?
Is the installation engineer sitting with you and completing an installation configuration list with you based on the information you provided? This configuration list can be imagined as a class, it is just a piece of paper , but it records the information of the PC you want to buy. For example, if you buy 10 machines with this configuration list, then these 10 machines are all composed according to this configuration list, so these 10 machines are of the same type. , can also be said to be of the same type. So what is an object? The instantiation result of a class is an object. The machine configured (instantiated) using this configuration sheet is an object, an entity that we can operate. 10 machines, 10 objects. Each machine is independent, which only means that they are of the same type. Any action taken on one of the machines will not affect the other 9 machines. However, when I modify the class, I add a or If one accessory is missing, then the nine installed machines will all change. This is the relationship between classes and objects (the instantiation result of a class is an object).
3.What is object-oriented programming?
Not to mention his concept, if you want to build a computer classroom, you must first have a room with N computers, N tables, N chairs, whiteboards, projectors, etc. What are these? As we just said, these are objects, entities that can be seen one by one. It can be said that the units of this computer classroom are these individual physical objects, and they collectively make up this computer classroom. Then we are doing programs, which What does it have to do with object-oriented? Developing a system program is similar to building a computer classroom. You abstract each independent functional module into a class and form an object. This system is composed of multiple objects. These objects can receive information, process data and send data to other objects. Sending messages and other interactions.
4. How to abstract a class?
As introduced above, the unit of object-oriented program is the object, but the object is instantiated by the class, so the first thing we have to do is how to declare the class. It is easy to make a class, as long as you master the basic Programming can be done by defining the rules of programming grammar, so what’s the difficulty? How many classes and objects should be used in a project, where the class should be defined, what kind of class is defined, how many objects are instantiated by this class, how many attributes are there in the class, how many methods are there, etc. , which requires readers to analyze, design and summarize practical problems in actual development.
Definition of class:
class class name {
}
Use a keyword class followed by a class name you want and a pair of curly brackets, so that the structure of a class is defined, as long as it is inside Just write the code, but what is written in it? What can I write? How to write a complete class? As mentioned above, the purpose of using a class is to make it instance an object for us to use. This requires knowing what kind of object you want. Like the installation configuration sheet we mentioned above, what is written on the installation configuration sheet? Is there anything about the machine? For example, a person is a target. How do you recommend a person you like to your leader? Of course, the more detailed the better:
First, you will introduce the person’s name, gender, age, height, weight, phone number, home address, etc.
Then, you have to introduce what this person can do, whether he can drive, speak English, use a computer, etc. As long as you introduce more, others will know more about this person. This is our description of a person. Now let us summarize, all objects we use to describe are similar. From the description of the person above, we can As you can see, making a class is divided into two parts from the definition point of view. The first is static description, and the second is dynamic description. Static description
is what we call attributes, as we see above , the person’s name, gender, age, height, weight, phone number, home address, etc.
Dynamic is the function of this human object. For example, this person can drive, speak English, can use a computer, etc. When abstracted into a program, we write the dynamic as a function or method. 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 person {
Member attributes: name, gender, age, height, weight, phone number, home address
Member methods: can drive, speak English, can use a computer
}
Attributes:
By using keywords in the class definition "var" is used to declare variables, that is, attributes of the class are created. Although initial values can be given when declaring member attributes, it is not necessary to give initial values to member attributes when declaring a class. For example, if Assign the name "Zhang San", then use this class instance to create dozens of people, and these dozens of people will be called Zhang San, so it is not necessary. We can just give the initial value of the member attribute after the object is created from the instance.
For example: var $somevar;
Method (member function):
By declaring a function in the class definition, a method of the class is created.
For example: function somefun (parameter list)
{ ... ... }
Code snippet
Copy the code
The code is as follows:
class Person{ //The following is human Member attributesvar $name; //The person’s namevar $sex; //The person’s gendervar $age; //The person’s age
function say(){
// The way this person can talk
echo "This person is talking";
} function run(){
//The way this person can walk
echo "This person is walking";
}
}
?>
The above is a declaration of a class, a class declared in terms of attributes and methods, but it is best not to give an initial value to the member attributes when declaring
, because the class we make is a description information, and it will be used in the future Instantiating objects, for example, if 10 personal objects are instantiated, then each of these 10 people will have a different name, gender, and age.
So it is best not to assign initial values to member attributes here, but Values are assigned to each object separately.
Using the same method, you can make the class you want, as long as you can use attributes and methods to describe the entity.
Define it as a class and instantiate the object.
In order to strengthen your understanding of classes, let’s make another class, a shape class. The range of shapes is a bit wider, let’s
make a rectangle, analyze it first, think about it from two aspects, rectangular What are the attributes? What functions does a rectangle have?
//Properties of rectangleLength of rectangle; Width of rectangle; //Method of rectanglePerimeter of rectangle;
Area of rectangle ;}
Code snippet
Copy code
The code is as follows:
class Rect{
var $kuan;
var $gao;
function zhouChang(){
Calculate the perimeter of the rectangle;
} function mianJi(){
Calculate the area of the rectangle;
}
}
?>