Php object-oriented full strategy (1) Basic object-oriented knowledge _ PHP Tutorial

Source: Internet
Author: User
Php object-oriented full strategy (1) Basic object-oriented knowledge. 1. object-oriented concept object-oriented programming (ObjectOrientedProgramming, OOP, object-oriented programming) is a computer programming architecture. one basic principle of OOP is computer programs. 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 to declare classes in PHP, and only use objects in your project.
And class. 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, the reader will try again.
Read and understand.
2. what is a class, what is an object, and 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, the class is
An independent program unit 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
It is like an abstraction of a problem domain or something in the implementation domain. it reflects the information that needs 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 objects.
And 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 PC machines in Zhongguancun, what do you do first when you get there,
Is the installation engineer sitting with you and completing an installation configuration ticket together with you based on the information you provide? 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, such
If you use this configuration item to buy 10 machines, these 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 a class is an object.
Machines configured (instantiated) by configuration sheets are objects, which are entities that we can operate on, 10 machines, and 10 objects. Each server sub-database is independent and can only indicate that they are in the same category.
The other 9 machines are affected. However, if I modify the class, that is, add one or fewer accessories to the configuration list, all the 9 machines that have been installed have changed, this is the relationship between the class and the object (the instantiation result of the class is the object ).
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 said, this is an object. we can see entities. we can say that the units in this computer classroom are entity objects.
The computer classroom is composed of the same, so we are working on programs. what is the relationship between this and object-oriented? Developing a system program is similar to building a computer classroom. you Abstract each independent function module into a class to form an object consisting of multiple objects.
In this system, these objects can receive information, process data, send information to other objects, and so on.
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, as long as you have mastered the basic program syntax
Rules can be implemented. where are the difficulties? 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, yes
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
What about 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 have mentioned above, what is written on an installation configuration sheet? what is the name of the host you have installed?
. For example, if a person is an object, how do you recommend a person you are optimistic about to your leadership? 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 want to introduce more, others will know more about this person. this is our description of a person.
To sum up, we can describe all objects using classes. from the above descriptions, we can see that a class is made, which is divided into two parts from the definition perspective, the first is static description, the second is dynamic description, and static description.
This is what we call Attributes. as we can see above, the 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
Both attributes and methods are written. attributes are also called member attributes of this class. 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 Zhang San, so there is no need
Yes, we can give the initial value of the member attribute after the instance outputs the object.
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)
{......}
Code snippet

The code is as follows:


Class Person {
// The following are the member attributes of a person.
Var $ name; // The 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 (){
// 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 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 our class is a descriptive information and will be used to instantiate the object.
For example, if 10 objects are instantiated, the names, genders, and ages of these 10 objects are different,
Therefore, it is best not to assign an initial value to the member attribute here, but to assign values to each object separately.
You can use the same method to make the desired class, as long as you can use the attributes and methods to describe the entity.
Define the object as a class to instantiate the object.
To enhance your understanding of the class, let's make another class and make a shape class. the shape range is wide and we
Let's just make a rectangle. let's analyze it first and think about what the attributes of the rectangle have in two aspects? All functions of rectangles are available.
What?

The code is as follows:


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;
}


Code snippet

The code is as follows:


Class Rect {
Var $ kuan;
Var $ gao;
Function zhouChang (){
Calculates the perimeter of a rectangle;
} Function mianJi (){
Calculates the area of the rectangle;
}
}
?>


If this class is used to create multiple rectangle objects, each rectangle object has its own length and width, which can be obtained from
The length of the week and the area have reached.
Class declaration here !!

Object-oriented Programming (Object Oriented Programming, OOP, object-oriented Programming) is a computer Programming architecture. a basic principle of OOP is computer Programming...

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.