Home  >  Article  >  Backend Development  >  PHP object-oriented guide (1) Basic knowledge of object-oriented

PHP object-oriented guide (1) Basic knowledge of object-oriented

黄舟
黄舟Original
2016-12-19 13:41:321352browse
  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 unit that can function as a subroutine. Composed of objects, 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 larger 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
    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 authorized objects. 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, all nine installed machines will be changed. 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. They together make up this computer classroom. So we are doing programs. This 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. It is composed of multiple objects. In this system, 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 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 Program syntax definition
    It can be done according to the righteous rules, 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 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 it? 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 classes 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 a 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, as we saw 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


    <?php 
    class Person{ 
    //下面是人的成员属性 
    var $name; //人的名字 
    var $sex; //人的性别 
    var $age; //人的年龄 
    //下面是人的成员方法 
    function say(){ 
    //这个人可以说话的方法 
    echo "这个人在说话"; 
    } function run(){ 
    //这个人可以走路的方法 
    echo "这个人在走路"; 
    } 
    } 
    ?>

    The above is a declaration of a class, a class declared from attributes and methods, but the member attributes are best in Don’t give an initial value when declaring
    , because the person class we make is a description information, and we will use it to instantiate objects in the future. For example,
    if 10 person objects are instantiated, then the name of each of these 10 people, Gender and age are different,
    So it is best not to assign initial values ​​to member attributes here, but to assign values ​​to each object separately.

    Using the same method, you can make the class you want. As long as you can describe the entity with attributes and methods, you can
  2. 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. Let’s analyze it first. Think about it from two aspects. The rectangle What are the attributes? What are the functions of a rectangle?

class 矩形 
{ 
//矩形的属性 
矩形的长; 
矩形的宽; 
//矩形的方法 
矩形的周长; 
矩形的面积; 
}

Code snippet
<?php 
class Rect{ 
var $kuan; 
var $gao; 
function zhouChang(){ 
计算矩形的周长; 
} function mianJi(){ 
计算矩形的面积; 
} 
} 
?>

If you use this class to create multiple rectangular objects, each rectangular object has its own length and width, you can calculate its own perimeter and area.

The above is the PHP object-oriented guide (1) The content of basic object-oriented knowledge. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

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