What does php object-oriented mean?

青灯夜游
Release: 2023-03-11 06:12:01
Original
4182 people have browsed it

In PHP, object-oriented is to abstract the problems to be dealt with into objects, and then solve the actual problems of the objects through the properties and behaviors of the objects. There are two important basic concepts in object-oriented: class and object. A class is the abstraction of an object, and the properties and methods of an object can be defined in a class. An object is an instance of a class, and a class can only be used after it is instantiated.

What does php object-oriented mean?

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

Object-oriented is to abstract the problems to be dealt with into objects , and then solve the actual problems of the object through the properties and behaviors of the object. What we usually call object-oriented should be accurately called object-oriented programming (OOP), which is part of object-oriented.

Object-oriented is mainly divided into three parts, as follows:

  • Object Oriented Analysis (OOA);

  • Object Oriented Design (OOD);

  • Object Oriented Programming (OOP).

The two important basic concepts in object-oriented are classes and objects:

1) Class

Everything in the world has its own attributes and methods, through which different things can be distinguished. For example, a person has attributes such as height, weight, and skin color, and can also perform activities such as eating, studying, and walking. These activities can be said to be functions that people have. People can be regarded as a class in the program, then a person's height can be regarded as an attribute in the class, and walking can be regarded as a method in the class.

In other words, a class is a collection of attributes and methods, which is the core and foundation of object-oriented programming. Through classes, scattered codes used to implement a certain function can be effectively managed. For example, create a sports class that includes 5 attributes; name, height, weight, age, and gender, and defines 4 methods: soccer, basketball, weightlifting, and high jump.

In short, a class is a collection of variables (class attributes) and functions (class methods) that act on these variables. Attributes and methods are the basis of a class.

2) Object

A class is an abstract model with a certain function. In practical applications, the class also needs to be instantiated, thus introducing the concept of an object.

The object is the product of instantiation of a class and is an entity. Still taking human beings as an example, there is nothing wrong with the sentence "Yellow race people are human beings", but conversely, "Humans are yellow race people", this sentence must be wrong. Because in addition to yellow people, there are also black people, white people, etc. Then "yellow race person" is an instance object of the "person" class.

The relationship between objects and classes can be understood this way: a class is the abstraction of an object, and the properties and methods of the object can be defined in the class; the object is an instance of the class, and the class can only be used after it is instantiated.

Characteristics of object-oriented programming

Object-oriented programming has three major characteristics: encapsulation, inheritance, and polymorphism, which cater to the emphasis on code in programming The need for reusability, flexibility, and scalability has established the position of object-oriented programming in programming.

1) Encapsulation

Encapsulation is to separate the use and implementation of a class, leaving only limited interfaces (methods) to communicate with the outside. For developers who use this class, they only need to know how to use this class, and do not need to care about how this class is implemented. Doing so allows developers to better focus on other things, while also avoiding the inconvenience caused by interdependencies between programs.

For example, when using a computer, we do not need to disassemble the computer to understand the specific use of each component. We only need to press the power button to start the computer, which reflects the benefits of packaging.

2) Inheritance

Inheritance means that a derived class (subclass) automatically inherits the properties and methods in one or more base classes (parent classes) and can override them. Write or add new properties or methods. Inheritance simplifies the creation of objects and classes and increases code reusability.

For example, class A has been defined, and class B is going to be defined next. Class B has many attributes and methods that are the same as class A. Then class B can be used to inherit class A, so that there is no need to Class B defines the existing properties and methods in class A, which can greatly improve the development efficiency of the program.

Inheritance is divided into single inheritance and multiple inheritance. PHP currently only supports single inheritance, which means that a subclass has only one parent class.

3) Polymorphism

The state of an object is changeable. An object can have different states compared to another object of the same class. Although they have the same properties and methods, they can have different states. In addition, a class can derive several subclasses. While retaining some properties and methods of the parent object, these subclasses can also define some new methods and properties, or even completely rewrite some existing properties of the parent class. Methods. Polymorphism enhances software flexibility and reusability.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of What does php object-oriented mean?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!