Home > Backend Development > PHP Tutorial > Usage of abstract classes and interfaces in PHP

Usage of abstract classes and interfaces in PHP

小云云
Release: 2023-03-22 13:06:01
Original
1810 people have browsed it

This article mainly shares with you the usage of abstract classes and interfaces in PHP. I hope it can help you. Let's first share with you the concept of abstract methods.

1. Abstract method

In a class, a method without a method body is an abstract method.

abstract Visibility function method name (parameter 1,...); // If visibility is not specified explicitly, the default is public

For example:

public function hello($args);
abstract function work();            // 修饰符abstract,也可以省略
Copy after login

2. Abstract class

abstract class class name{

Attribute;

Method ;

                                                                                                                                                                                                                                                                                           

##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 , needs to be rewritten.

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

#Abstract methods can be used when certain methods must be encapsulated in the control subclass.

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

  • Example: Declare a human with an abstract method that works.

  • Declare a php lecturer class and override the method to work.
  •   abstract class People{
            protected $age=22;
            public $height=1.70;
            abstract function work();
        }
        class PhpTeacher extends People{
            function work(){
                echo "真不是php";
            }
        }
    Copy after login

    3. Interface

If all methods in a class are abstract methods and have no member attributes, then this class is It's called an interface.

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

The role of interface: Although PHP classes have single inheritance, multiple inheritance can be achieved through interfaces.

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

Interface implementation (implements):

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

Inherit the class and implement the interface at the same time:

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

4. The difference between abstract classes and interfaces

  • The interface is a special abstract class that only contains Abstract method, no member properties.

  • When a class implements an interface (implements), it must fully implement all methods in the interface; when a class inherits (extends) an abstract class, Just rewrite the abstract methods you need.

  • Abstract classes can only have single inheritance, but interfaces have multiple inheritance, and the class's implementation of the interface also has multiple implementations.

##Related recommendations:

php abstract Detailed explanation of classes

Detailed explanation of abstract classes and interfaces in PHP

Abstract classes and interfaces in PHP

The above is the detailed content of Usage of abstract classes and interfaces in PHP. 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