Detailed Explanation of PHP Abstract Class and Interface Usage Examples

  • 2021-12-13 07:33:18
  • OfStack

This article illustrates the use of PHP abstract classes and interfaces. Share it for your reference, as follows:

Preface

It is estimated that most people are no strangers to oop. Some people learn many other languages besides PHP, and they will find the difference between php. The grammar may be extremely ugly, but it does not prevent it from becoming the best language in the world (cult language). PHP allows constants as part of an interface, but 10 points are important for understanding abstractions.

On the computer, the understanding of abstraction is different from the abstract concepts we use every day in natural language. For example, when we refer to animals such as'dog ', 'cat', we will say'that dog/cat ', and they are concrete examples of having such characteristics as dog/cat. But we can't think of cats and dogs as Class 1, that is to say, you can't say dogs are cats, we can define both dogs and cats as animals. Therefore, we define abstraction as the basic feature of an object, which clearly distinguishes it from other objects.

Abstract class

Abstract classes can have non-abstract methods. But there can only be abstract methods in the interface. A class that declares the existence of a method but does not implement it is called an abstraction class (abstract class), which is used to create a class that embodies some basic behavior and declares methods for the class, but cannot implement the class in the class. Cannot create an instance of the abstract class. However, you can create a variable of type 1 abstraction class and point to an instance of a concrete subclass. You cannot have an abstraction constructor or an abstraction static method. Subclasses of the Abstract class provide implementations for all abstraction methods in their parent class, otherwise they are also abstraction classes for. Instead, implement the method in a subclass. Other classes that know their behavior can implement these methods in the class.

Look at a common class first:


<?php
class appletree{
  privated $catch;
  piblic function tree($sweet){
    $this->catch=$sweet;
    return $this->catch;
  }
$apple=new appletree();
$eat=$apple->tree('this apple is sweet');
echo $eat;
?>

Look at another abstract class:


//appletree.php:
<?php
abstract class appletree{
  privated $catch;
  abstract public function tree1($sweet);
  public function tree2(){
    echo'smell';
  }
  public function _construct(){
    //......
    }
}
?>


<?php
include_once('appletree.php');
class anothertree extends appletree{
  public function tree1($sweet){
    $this->catch='this apple is';
    return $this->catch.$sweet;
    }
  }
$apple=new appletree();
echo $apple->tree1('sweet');
?>

As you can see from the general and abstract classes:

Abstract classes and abstract methods must have abstract previously defined and called extends. There can be concrete methods in an abstract class, and concrete methods can be instantiated in an abstract class, but abstract methods cannot be instantiated in an abstract class.

Interface

Interface is also an indispensable part of oop pattern, and interface (interface) is a variant of abstract class. In an interface, all methods are abstracted. Multiple inheritance can be achieved by implementing such an interface. All methods in the interface are abstracted, and none of them have a program body. Interface can only define static final member variables. The implementation of an interface is similar to that of a subclass, except that the implementation class cannot inherit behavior from the interface definition. When a class implements a special interface, it defines (that is, gives the program body) all the methods of that interface. It can then call the interface's methods on any object of the class that implements the interface. Because of the abstract class, it allows the interface name to be used as the type of reference variable. The usual dynamic binding will take effect. References can be converted to or from interface types, and the instanceof operator can be used to determine whether an object's class implements an interface.

Specific examples are as follows:

//fruit.php


<?php
interface fruit{
  public function apple($sweet);
  public function orange();
  }
?>


<?php
include_once('fruit.php');
class fruittree implements fruit{
  privated $catch;
  public function apple($sweet){
    $this->catch='this fruit is';
    rerurn $this->catch.$sweet;
    }
  public function orange(){
    return 'this orange is sweet';
    }
  }
$tree=new fruittree();
echo $tree->apple('sweet');
echo $tree->orange();
?>

Interfaces and constants

An example seen on the Internet: (Interface name and constant should be separated by spacer ':', static constant can be used in interface, variable can't)


<?php
interface TestInterface
{
  const CONSTVAR = 'aaa';
  static staticvar = 111;
  public function alert($str);
}
class TestClass implements TestInterface
{
  const CONSTVAR = 'bbb';
  public function __CONSTRUCT()
  {
    echo TestInterface::CONSTVAR;
  }
  public function alert($str)
  {
    echo $str;
  }
  public function __DESTRUCT()
  {
  }
}
$test1 = new TestClass();
?>

We can see the difference between interfaces and abstract classes:

1. There are no concrete methods in the interface, they are all abstract methods.
2. The interface call is implements, and the abstract class is extends.
3. Member variables (including class static variables) cannot be declared in the interface, but class constants can be declared. Abstract classes can declare various types of member variables to encapsulate data.
4. Interfaces have no constructors, but abstract classes can have constructors.
5. Methods in interfaces are public by default, while methods in abstract classes can be decorated with private, protected and public.
6. A class can implement multiple interfaces at the same time, but a class can only inherit from one abstract class.

Use selection

If you want to create a model that will be adopted by a number of closely related objects, you can use abstract classes. If you want to create functions that will be adopted by 1 unrelated objects, use interfaces.

If you must inherit behavior from multiple sources, use interfaces.

If you know that all classes share a common behavior implementation, use an abstract class and implement the behavior in it.

For more readers interested in PHP related content, please check the topics on this site: "Introduction to php Object-Oriented Programming", "Encyclopedia of PHP Array (Array) Operation Skills", "Introduction to PHP Basic Grammar", "Summary of PHP Operation and Operator Usage", "Summary of php String (string) Usage", "Introduction to php+mysql Database Operation" and "Summary of php Common Database Operation Skills"

I hope this article is helpful to everyone's PHP programming.


Related articles: