The difference between abstract classes and interfaces in php

不言
Release: 2023-03-23 08:12:01
Original
1912 people have browsed it

The difference between abstract class and interface php

tags: abstract class interface abstract class and interface php


## Introduction: This is a question that is often asked in interviews and is also a classic question. We try our best to quote official authoritative instructions or conduct experiments to prove the accuracy of the content stated in this article.

Abstract class

Please check the document for the official description. The following is a simplified version of the official description:

  • Classes defined as abstract cannot be Instantiate. Any class must be declared abstract if at least one method in it is declared abstract. (An abstract class may not have abstract methods, but an abstract class still cannot be instantiated.) A method defined as abstract only declares its calling method (parameters), and

    cannotdefine its specific function implementation. For example,

  • abstract class  AbstractClass
     {
        // 强制要求子类定义这些方法,且不能定义具体功能 注意没有大括号{}
         abstract protected function  getValue ();
        abstract protected function  prefixValue ( $prefix );
    
         // 普通方法(非抽象方法)
         public function  printOut () {
            print  $this -> getValue () .  "\n" ;
        }
    }
    Copy after login
  • When inheriting an abstract class, the non-abstract subclass must define all abstract methods in the parent class; in addition, the access control of these methods must be consistent with Same as in the parent class (or more relaxed). For example, if an abstract method is declared as protected, then the method implemented in the subclass should be declared as protected or public, and cannot be defined as private.

  • In addition, the calling method of the method must match, that is, the type and the number of required parameters must be consistent. For example, the subclass defines an optional parameter (

    is similar to function eat(

    b=1) The $b is the optional parameter ), but it is not included in the declaration of the abstract method of the parent class, then there is no conflict between the two declarations. This also applies to constructors since PHP 5.4. Constructor declarations before PHP 5.4 could be different.

  • Supplement:


    1. Abstract classes can have member attributes.

    2. Someone asked: Can an abstract method be defined as private? The answer is no, because the purpose of the abstract interface is to abstract the class model for inheritance. It is defined as private and cannot be accessed from outside. It deviates from the design purpose. The following error will be reported

    abstract class Sutdent extends Human{
        abstract private function study();}
    Copy after login
Fatal error: Abstract function Sutdent::study() cannot be declared private in D:\11\index.php on line 10

  1. Abstract classes can inherit abstract classes,

    and cannotoverride abstract methods of abstract parent classes. Such usage can be understood as an extension of abstract classes. For example,

  2. abstract class  Human{
        abstract function eat();}abstract class Sutdent extends Human{
        abstract function study();
        //abstract function eat(); 若重写抽象父类的抽象方法eat()会报错}
    Copy after login
If you override the abstract method of the abstract parent class, the following error will be reported

##Fatal error: Can't inherit abstract function Human::eat( ) (previously declared abstract in Sutdent) in D:\11\index.php on line 11

Interface

1. Definition of interface

    Using interface (interface), you can specify which methods a certain class must implement, but you do not need to define the specific content of these methods.
  • The interface is defined through the interface keyword, just like defining a standard class, but all methods defined in it are empty.
  • All methods defined in the interface must be public. This is a characteristic of the interface. Protected and private will report an error (
  • Fatal error: Access type for interface method

    ) .

  • Constant: Constants can also be defined in interfaces. Interface constants are used exactly the same as class constants, but they cannot be overridden by subclasses or subinterfaces. (It is not recommended to use it this way. I really can’t think of any meaning, and it is easy to cause confusion with abstract classes)
  • interface Play  {  
        const LEVEL=10;  
        public function PlayLOL();  
        public function PlayFootball();  }
    Copy after login
Implementation of the interface

To implement an interface , using the implements operator. Non-abstract classes must implement all methods defined in the interface, otherwise a fatal error will be reported. A class can implement multiple interfaces. Use commas to separate the names of multiple interfaces.

Replenish:
You can inherit abstract classes and implement interfaces at the same time. Extends must be written in front.
Abstract classes implement interfaces, and there is no need to redo the methods.

  • #When implementing multiple interfaces, methods in the interfaces cannot have the same name.

  • Interfaces can also be inherited, by using the extends operator.

  • To implement an interface, a class must use methods that are exactly the same as those defined in the interface. Otherwise a fatal error will result.

interface Play  {  
    const LEVEL=10;  
    public function PlayLOL();  
    public function PlayFootball();  } 

interface Read  {  
    public function ReadNovel();  } 

abstract class  Human{
    abstract function eat();}//抽象类可以实现接口后不实现其方法,可以继承一个抽象类的同时实现多个接口注意必须要把extends语句写在implements前面,否则会报错abstract class Sutdent extends Human implements Play,Read{
    abstract function study();}
Copy after login
Inheritance of interface

An interface can inherit another or multiple interfaces, use the extends keyword, and separate multiple ones with ',', but You cannot implement another interface, and of course you cannot inherit an abstract class (error when inheriting an abstract class:

Fatal error: PlayGame cannot implement Human - it is not an interface in D:\11\index.php on line 10)

interface Play  {   
    public function PlayFootball();  }interface Play1  {   
    public function PlayFootball();  }interface PlayGame extends play,Play1{  
    public function PlayLOL();  }
Copy after login
Summary

Usually I write the similarities and differences here, but I don’t write them, hehe, because I think the above is detailed enough.

Let’s briefly summarize: abstract classes are generally used to define a type of entity What is it? It includes attributes, abstract methods and non-abstract methods. An interface is used to define what a class of entities can do. It is generally believed that it only has abstract methods, and constants are rarely used. Related recommendations:

The difference between php abstract classes and interfaces

The above is the detailed content of The difference between 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!