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

Can php define abstract methods?

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

Share

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

This article is about whether php can define abstract methods. Xiaobian thinks it is quite practical, so share it with everyone for reference. Let's follow Xiaobian and have a look.

PHP defines abstract methods. In PHP5, in order to facilitate class inheritance, the concept of abstract method is introduced; abstract method is a method without method body. When declaring abstract method in PHP, use "abstract" keyword modification, syntax "abstract access modifier function method name (parameter list);".

Operating environment of this tutorial: Windows 7 system, PHP7.1 version, DELL G3 computer

PHP defines abstract methods.

In object-oriented languages, a class can have one or more subclasses, and each class should have at least one public method as an entry point for external code.

Abstract classes and abstract methods are concepts introduced in PHP 5, mainly to facilitate class inheritance.

Abstract methods and abstract methods

Abstract methods are methods without a method body. The so-called no method body refers to the fact that there are no curly braces { } and their contents when declaring a method, but a semicolon is added directly after the method name. In addition, abstract methods are declared using the "abstract" keyword. The format is as follows:

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

Examples:

abstract function fun1();abstract function fun2();

The above example is the abstract method "fun1()" and "fun2()" modified by "abstract" without method body. Don't forget that there is also a semicolon after the abstract method;

So what are abstract classes?

As long as there is a method in a class that is abstract, then the class must be defined as an abstract class, and the abstract class must also be modified with the "abstract" keyword; in an abstract class, there can be methods and member attributes that are not abstract, but as long as there is a method that is abstract, the class must be declared as an abstract class, and modified with the "abstract" keyword.

abstract class Demo{ var $test; abstract function fun1(); abstract function fun2(); function fun3(){ ... }}

In the above example, an abstract class "Demo" is defined and decorated with "abstract". In this class, a member attribute "$test" is defined, and two abstract methods "fun1" and "fun2" are defined. There is also a non-abstract method fun3();

So how do we use abstract classes? The most important point is that abstract classes cannot generate instance objects, so they cannot be used directly. We mentioned many times before that classes cannot be used directly. What we use is objects instantiated by classes. So abstract classes cannot generate instance objects. What is the use of declaring abstract classes? We use abstract methods as templates for subclass overloading. Defining abstract classes is equivalent to defining a specification that requires subclasses to comply with. After subclasses inherit abstract classes, abstract methods in abstract classes are implemented according to the needs of subclasses. Subclasses must implement all abstract methods in the parent class, otherwise abstract methods still exist in the subclass, then the subclass is still abstract class, or can not instantiate the class; why do we have to inherit from abstract classes? Because sometimes we have to inherit some functions from abstract classes, otherwise you can't implement these functions. If you inherit abstract classes, you have to implement abstract methods in classes.

Thank you for reading! About "php can define abstract methods" this article is shared here, I hope the above content can be of some help to everyone, so that everyone can learn more knowledge, if you think the article is good, you can share it to let more people see it!

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