Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand PHP abstract classes and abstract methods

2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the knowledge of "how to understand PHP abstract classes and abstract methods". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

In the last article, we introduced the interface, which mentioned that the interface can be understood as a special abstract class, so what is an abstract class? if we want to understand what an abstract class is, we should first talk about abstract methods. The emergence of abstract classes and abstract methods is mainly to facilitate class inheritance, so let's take a look at what is abstract methods and what is abstract classes.

What is an abstract method?

In our previous study, a class can have multiple subclasses, and we define abstract methods to facilitate class inheritance. The method without method body defined in our class is abstract. So what is the method body? The method body refers to the curly braces and the contents of the curly braces when the class is declared, and no method body means that there is no such content, but directly ends with a semicolon after the declaration. This is called an abstract method.

It is important to note that abstract methods are decorated with the keyword "abstract" when declared. Its syntax format is as follows:

Abstract access modifier function method name 1 (parameter list); abstract access modifier function method name 2 (parameter list)

The understanding of abstract methods is relatively simple, so let's take a look at what abstract classes are.

What is an abstract class?

Since we already know what an abstract method is, the definition of an abstract class is easy to understand. when a method in a class is an abstract method, we define the class as an abstract class. At the same time, an abstract class also needs to be modified with the "abstract" keyword. In an abstract class, there can be member properties that are not abstract methods, but once a method is an abstract method, the class is an abstract class.

It should be noted that the member methods of abstract classes are not all abstract methods, the access rights of member methods and properties that are not abstract methods cannot be private, and methods in abstract classes also need to be inherited by subclasses.

Let me give you an example:

Abstract class demo {var $test;abstract function fun1 (); abstract function fun2 ();}

The characteristics of abstract class

Both abstract classes and abstract methods need to be decorated with abstract

Abstract methods must be in abstract classes, and there can be no abstract methods in abstract classes.

Abstract classes cannot directly create objects (instantiation)

If a subclass of an abstract class overrides all abstract methods, it is a concrete class; if a subclass of an abstract class does not override all abstract methods, then the subclass is still an abstract class

An abstract class contains abstract methods that cannot be implemented, which means that the abstract class cannot be instantiated, so we cannot create an object, so we cannot use it directly. So since we can't use it directly, what problem can we solve by using abstract classes?

Next, let's take a look at the example, define an abstract class, and then use another class to inherit the abstract class and implement the abstract methods in the abstract class.

Examples are as follows:

Output result:

From the above example, we implement to define an abstract class and then use another class to inherit the abstract class and implement the abstract methods in the abstract class.

As mentioned above, the previous article talked about the knowledge of the PHP interface, which said that the interface can be understood as a special abstract class, so what are the differences and similarities between the PHP interface and the abstract class? Next, let's take an inventory. It is recommended that you first take a look at "five minutes to show you the interface interface declaration and application in PHP (detailed explanation of examples)", which introduces the PHP interface.

The difference between PHP interface and abstract class

Abstract classes can have properties, ordinary methods, abstract methods, but interfaces cannot have properties, ordinary methods, and can have constants.

There may not be abstract methods in the abstract class, but there must be "abstract" methods in the interface.

An abstract class is declared before the class with the abstract keyword, and the interface is declared as a class with class, and the interface is declared with interface, but not with class, because the interface is not a class.

Abstract class is to use the extends keyword to let the subclass inherit the parent class, and then implement the detailed abstract method in the subclass. The interface is the detailed method of using implements to let the ordinary class implement the interface in the class, and the interface can implement multiple methods at one time, and separate each interface with a comma.

Similarities between PHP interface and abstract class

Are used to declare something, specification names, parameters, the formation of modules, there are no detailed implementation details.

It is through the class to achieve the relevant details.

Syntactically, abstract methods of abstract classes, like interfaces, cannot have method bodies, that is, {} symbols.

Inheritance can be used, interfaces can inherit interfaces to form new interfaces, and abstract classes can inherit abstract classes to form new abstract classes.

That's all for "how to understand PHP Abstract classes and Abstract methods". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report