Home >Backend Development >PHP Tutorial >PHP object-oriented guide (1) Basic object-oriented knowledge_PHP tutorial

PHP object-oriented guide (1) Basic object-oriented knowledge_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:44:04743browse

1. The concept of object-oriented
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 Composed of units or objects that function as subroutines, OOP achieves the three goals of software engineering: reusability, flexibility, and scalability. 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 larger projects, you may want 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 it 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
The concept of a 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 An encapsulation of a set of services that operate on these properties. 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 are probably their definitions. Maybe you are new to object-oriented. 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, What is your first step when you get there?
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 will all be composed according to this configuration list, so this The 10 machines are of one type, or one category. 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 If there is one or one less accessory, 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. It can be said that the units of this computer classroom are these entity objects. They together constitute this computer classroom. So we are doing programs, what does this 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 messages to each other. Other objects send messages and so on interact with each other.
4. How to abstract a class?
As mentioned 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 grammar definition rules, you can do it. So what is the difficulty? How many classes and objects should be used in a project, where the class needs to be defined, what kind of class is defined, how many objects are instantiated by this class, how many attributes are there in the class, and how many methods are there? Wait, this requires readers to analyze, design and summarize actual 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, such a class structure It’s defined, and you just need to write code in it, 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 instantiate objects 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 above As you can see from the description, making a class is divided into two parts from the definition point of view. The first is a static description, and the second is a dynamic description. The static description
is what we call attributes, like What we saw above is the person’s name, gender, age, height, weight, phone number, home address, etc.
Dynamically speaking, it 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 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 method: can drive, speak English, can use computer
}
Attributes :
By using the keyword "var" in the class definition to declare variables, the attributes of the class are created. Although the initial value can be given when declaring the member attribute, the initial value of the member attribute is given when declaring the class. It is not necessary. For example, if
a person's name is assigned "Zhang San", then use this class instance to create dozens of people, and these dozens of people will be named Zhang San, so there is no need
to do it. We can just give the initial value to 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 code

The code is as follows: class Person{
//The following are the member attributes of the person
var $name; //The name of the person
var $sex; / /Person’s gender
var $age; //Person’s age
//The following is the person’s member method
function say(){
//How this person can speak
echo "This person is talking";
} function run(){
//How 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 we do The person class is a description information, which can be used to instantiate objects in the future. For example,
if 10 person objects are instantiated, then the name, gender, and age of each of these 10 people will be different.
So it is best not to assign initial values ​​to member properties here, but to assign values ​​to each object separately.
Using the same method, you can create the class you want. As long as you can use attributes and methods to describe the entity, you can
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
just make a rectangle. Let’s analyze it first and think about it from two aspects. Analysis, what are the properties of a rectangle? What are the functions of rectangles
?



Copy code
The code is as follows:

class Rectangle
{
//Properties of rectangle
Length of rectangle;
Width of rectangle;
//Methods of rectangle
Perimeter of rectangle;
The area of ​​the 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;
}
}
?>

If you use this class to create multiple rectangle objects, each rectangle Each object has its own length and width, and you can calculate its own
perimeter and area.
That’s it for the class declaration! !

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/320659.htmlTechArticle1. Object-oriented concept Object-oriented programming (Object Oriented Programming, OOP, object-oriented programming) is a Computer programming architecture, one of the basic principles of OOP is that computer programs...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn