Home > Backend Development > PHP Tutorial > The difference between abstract classes and interfaces in php

The difference between abstract classes and interfaces in php

不言
Release: 2023-03-24 12:34:01
Original
2246 people have browsed it

This article mainly introduces the difference between abstract classes and interfaces in PHP. It has certain reference value. Now I share it with you. Friends in need can refer to it

The difference between abstract classes and interfaces


Abstract class:

  • Single inheritance, use the extends keyword

  • Can have constant and variable attributes

  • Can have constructor methods

  • Methods in abstract classes can use the public, static, private, and protect keywords

  • The corresponding logic can be implemented in the method

  • There cannot be a method body in an abstract method

Interface:

  • Multiple inheritance can be achieved by using the implements or extends keywords to inherit

  • You can have constants, but you cannot have variables

  • No constructor method

  • Methods are generally modified with the public keyword

  • Only method name, no method body

##Neither abstract class nor interface can be instantiated

The difference between abstract class and interface

Abstract class:

  • Single inheritance, using the extends keyword

  • can have constant and variable attributes

  • Can have constructor methods

  • ##Methods in abstract classes can use public, static, private , the corresponding logic can be implemented in the protect keyword

  • method

  • in the abstract method There cannot be a method body

  • Interface:

  • Can achieve multiple inheritance, use implements or extends keyword inheritance

  • Can have constants, but cannot have variables

  • No constructor

  • Methods are generally modified using the public keyword

  • Only method name, no method body

  • Neither abstract classes nor interfaces can be instantiated

Related recommendations:

Analysis of the difference between Redis and Memcache

Usage of abstract classes and interfaces in PHP

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