Home > Backend Development > PHP Tutorial > How to define classes and objects in php

How to define classes and objects in php

怪我咯
Release: 2023-03-11 20:22:02
Original
3029 people have browsed it

In the process of learning PHP, I started from scratch; from not being able to understand the first book, to being impressed by the second book, to the simple operation statements of the third book... I can’t remember How many books have Qing read? However, when applying, every time I come to some key knowledge points and core technologies, I will have a vague feeling about their definition and usage, so I deliberately write down the knowledge points that I think are more important as online notes, so that I can review them frequently.

Process-oriented definition:

"Process-oriented" is a programming idea centered on event, which is to analyze the steps required to solve the problem, and then use functions Implement these steps step by step and call them one by one when using them.

The process-oriented programming method focuses on the use of data. During the programming process, flow charts are used to assist program design, and then structured programming statements are used to write programs. The process-oriented programming has a very strict logical structure, but the data management is somewhat confusing. When the size of the program is not very large, the process-oriented programming method can organize the program structure well according to modules and functions because the program flow is clear.

Object-orientedDefinition:

"Object-oriented" decomposes the transactions that constitute the problem into individual objects. The purpose of establishing objects is not to complete a step, but to describe The behavior of a transaction throughout the entire problem-solving process.

Object-oriented programming is based on process-oriented programming. The important change is that in the object-oriented programming process, the program will be designed around the data being operated, rather than the operation itself. Object-oriented programming uses classes as the basic unit of program construction, and has the characteristics of encapsulation, data abstraction, inheritance, polymorphism and so on.

Class:

In object-oriented programming, class is a very important core concept. From a programming perspective, a class is a collection of objects with the same semantic properties. The so-called same semantic characteristics refer to:

Objects in the same class have the sameproperties;
Objects in the same class have the same method;
Objects in the same class Objects obey the same semantic rules.

In PHP we use the keyword class to create a new class, and the first letter of the class name must be capitalized. Class members include member variables and member methods. The syntax format for defining a class is as follows:

class 类名称{
    //成员变量列表
    function 成员方法1([参数1,参数2,……]){
    //成员方法1的具体实现
    }
    function 成员方法2([参数1,参数2,……]){
    //成员方法2的具体实现
    }
   //其他成员方法
}
Copy after login

class Dabing { The following uses the keyword class to create a Dabing class. The code is as follows:

   //成员属性…
   //成员方法…
 }
Copy after login

This line of code only implements a class structure named "Dabing" and nothing else.

The above is the detailed content of How to define classes and objects in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template