What is the role of php abstract class

爱喝马黛茶的安东尼
Release: 2023-02-23 12:20:01
Original
5417 people have browsed it

What is the role of php abstract class

1. What is an abstract method?

The method we define in the class without a method body is an abstract method. The so-called no method body means that there are no curly braces and the contents inside when declaring, but directly adding a semicolon after the method name when declaring. In addition, when declaring an abstract method, the method also needs to add a keyword. "abstract" to modify.

For example:

abstract function fun1(); 
abstract function fun2();
Copy after login

2. What is an abstract class?

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

Related recommendations: "PHP Tutorial"

For example:

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

3. What is the role of abstract classes?

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

So how do we use abstract classes? The most important point is that abstract classes cannot produce instance objects, so they cannot be used directly. We have mentioned many times before that classes cannot be used directly. We use objects instantiated through classes.

So abstract classes cannot produce instance objects, what is the use of declaring abstract classes? We use abstract methods as templates for subclass overloading. Defining an abstract class is equivalent to defining a specification. This specification requires subclasses to comply. After the subclass succeeds the abstract class, the abstract class in the abstract class Methods are implemented as needed by subclasses. The subclass must implement all abstract methods in the parent class. Otherwise, if there are still abstract methods in the subclass, then the subclass will still be an abstract class and cannot instantiate objects;

Why do we have to start from What about inheritance in abstract classes? Because sometimes if we want to implement some functions, we must inherit from an abstract class, otherwise you will not be able to implement these functions. If you inherit an abstract class, you must implement the abstract methods in the class;

For example :

<?
abstract class Demo
{
    var $test; 
    abstract function fun1();
    abstract function fun2(); 
    function fun3() {
        ...
    }
} 
$demo=new Demo(); //抽象类不能产生实例对象,所以这样做是错的,实例化对象交给子类
 
class Test extends Demo
{
    function fun1() {
        ...
    } 
    function fun2() {
        ...
    }
} 
$test = new Test(); //子类可以实例化对象,因为实现了父类中所有抽象方法
?>
Copy after login

4. Characteristics of abstract classes:

Abstract classes cannot be instantiated and can only be inherited.

Abstract classes do not necessarily have abstract methods. A class with abstract methods must be an abstract class.

The visibility of abstract methods cannot be private.

Abstract methods are in subclasses and need to be rewritten.

5. When do you need to use abstract classes?

There is a method, but I don’t know how to write the method body. When this method must be included in the subclass, it is encapsulated into an abstract method and the class is an abstract class.

When certain methods must be encapsulated in the control subclass, abstract methods can be used.

When it is necessary to control a class that can only be inherited and cannot be instantiated.

6. Interface

If all methods in a class are abstract methods and have no member attributes, the class is called an interface.

interface Common{
    abstract function work();
    abstract function test($args);
}
Copy after login

Interface inheritance (extends):

Interface inheritance interface interface interface name extends parent interface name

Note: Class inheritance is single inheritance (there can only be one parent class), but the inheritance of the interface is multiple inheritance, and the implementation of the interface by the class is also multiple implementation.

Interface implementation (implements):

Class implements the interface class class name implements interface name 1, interface name 2, ...

Inherits the class and implements the interface at the same time:

Class inherits the parent class and implements the interface at the same time class class name extends parent class name implements interface name

7. The difference between abstract class and interface

Interface is A special abstract class whose interface only contains abstract methods and no member attributes. When a class implements an interface, it must fully implement all methods in the interface;

When a class inherits (extends) an abstract class, it only needs to rewrite the abstract methods that need to be used. Abstract classes can only have single inheritance, but interfaces have multiple inheritance, and the class's implementation of the interface also has multiple implementations.

The above is the detailed content of What is the role of php abstract class. 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
Popular Tutorials
More>
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!