Php object-oriented programming-three features of oop (encapsulation-inheritance-polymorphism)

Source: Internet
Author: User
Not all object-oriented languages have the concept of classes. The concept of class exists in the traditional oop language

Not all object-oriented languages have the concept of classes. The traditional oop language has the concept of class.

Class: Used to abstract objects. The next definition defines the attributes and capabilities (methods) of certain things ).

Abstract: The process of turning a real or logical world entity into an object of a programming language. It can also be called the encapsulation process. Make certain data and some operations into a unified whole (object)

Three features of OOP:


Defines the process of a class.

Private members should be used whenever possible. if users need to perform operations, corresponding operation interfaces should be provided.


If there is an overlap between attributes and methods in oop writing, duplicate code is extracted to form an object, and then the object that requires repeated code is inherited from this object.


A method has multiple forms of members.

Note: The above code cannot be executed, but it is used to simulate different forms of a method.


Object-oriented inheritance refers to the inheritance of objects from another object. you can take all the methods and attributes in another object for your own use.

In terms of syntax, the class is used for inheritance.

Use the keyword extends to inherit between classes.

Then, an inherited object can be obtained through instantiation.

In this way, the objects instantiated through Class D have all the attributes of class C objects.

Methods can also be inherited,

However, inheritance does not mean copying the code of the parent class to the subclass. it means that a relationship is maintained between the parent class and the subclass, and the member inheritance is achieved through this relationship.

Member control during inheritance:

All Public areas can be accessed (in this class, the parent class, subclass class, and out-of-class)

Private can be accessed in this class (in this class)

Protected refers to being accessible within the entire class inheritance chain. (This class, parent class, subclass)


Private members cannot be inherited, but can be accessed within the subclass.

Protected members can be inherited, but can only be accessed within the class

Common members can be inherited and can be both in-class and out-of-class members.

Note: Is access control at the object or class level?

It is to determine whether a member can be accessed from the class perspective, rather than from the object perspective. Because the same attribute is sometimes accessible and sometimes inaccessible, depending on the location of the access code.


If a class does not involve inheritance, use private to control member permissions.

If inheritance is involved, the protected permission will be used whenever possible.

# P # Three features of oop-inheritance and use details # e #

What should I do if a conflict occurs during inheritance?

Member name conflict

If a Member conflict occurs, the child class overwrites the code of the parent class.

If the child class cannot be found, it will go to the parent class for search:

Access permission conflict,

What should I do if the names are the same but the access control is different?

In the case of inheritance, the access level of members with the same name is required. the subclass level should be higher than the parent class level. If the parent class is a protected subclass, it should be protected or public.


To access members of the parent class with the same name in the class, use the parent keyword instead of the parent class. To access the parent class resources (static methods, methods, static attributes, constants ).

For example, the constructor for accessing the parent class:

Self: Replace this class

In Php, class inheritance is a single inheritance. it means that a class can only inherit another class and cannot directly inherit multiple classes at the same time.

Inherited in normal development:

Manage multiple tables in a project, add, delete, modify, and query each table, and define operations for each table as a class. They can all inherit a base class (db class)

# P # Three features of oop-final Class-abstract class-interface # e #

Class that cannot be inherited.

The Final class that cannot be inherited. the defined method is very simple. add the final key before the class.

An error occurs during inheritance.


Final can also be written before the method.

This method cannot be rewritten. However, the class can be inherited.

Class that can only be inherited.

Objects cannot be instantiated and can only be inherited by other classes.

This type is called an abstract class and is implemented using the abstract keyword.

Abstract class name {}

Abstract methods are usually defined in abstract classes (methods with only the method name and no method body ). Specifies the methods required to inherit the subclass of this abstract class. In other words, all abstract methods of this abstract class must be implemented to inherit the subclass of this abstract class.

Generally, the operation interfaces of different operation media are the same in the form of abstract classes, for example (the same database operation, but different database types:

Note: apart from abstract methods, other non-abstract methods and attribute definitions can exist in abstract classes.

Abstract classes also fall into the category of classes, but they cannot be inherited. You cannot use multiple classes to specify the content of a class.

Interface: interface

Used to specify the structure of a class. It can use multiple interfaces to define the structure of a class at the same time.

Interface definition:

Interface name {Interface member} Interface member: Use of constants and methods interfaces. the Interface must be implemented using classes to make sense: Class name implements Interface name list {Class member, you must implement incomplete methods in the interface .}

Does Php support multi-inheritance?


How to simulate multi-inheritance?

You can use the interface technology for simulation.

CAN interfaces be implemented with each other?

Can inherit from each other. Use the extends keyword.

Is the interface a class? Is it an abstract class? Are all abstract method classes?

The interface is not a class, not an abstract class. However, it can be understood that all abstract classes are abstract methods.

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: 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.