Php 简明教程

PHP – Interfaces

正如类是其对象的一个模板一样,PHP 中的 interface 可称为类模板。我们知道当一个类被实例化时,该类中定义的属性和方法对它可用。类似地,PHP 中的接口声明了方法及其参数和返回值。这些方法没有任何方法体,即在接口中没有定义任何功能。

Just as a class is a template for its objects, an interface in PHP can be called as a template for classes. We know that when a class is instantiated, the properties and methods defined in a class are available to it. Similarly, an interface in PHP declares the methods along with their arguments and return value. These methods do not have any body, i.e., no functionality is defined in the interface.

一个 concrete 类必须实现接口中的方法。换句话说,当一个类实现一个接口时,它必须为接口中的所有方法提供功能。

A concrete class has to implement the methods in the interface. In other words, when a class implements an interface, it must provide the functionality for all methods in the interface.

接口的定义方式与类的定义方式相同,只是使用了关键字 interface 来代替类。

An interface is defined in the same way as a class is defined, except that the keyword "interface" is used in place of class.

interface myinterface {
   public function myfunction(int $arg1, int $arg2);
   public function mymethod(string $arg1, int $arg2);
}

注意,接口中的方法没有任何功能。这些方法的定义必须由实现此接口的类提供。

Note that the methods inside the interface have not been provided with any functionality. Definitions of these methods must be provided by the class that implements this interface.

当定义一个子类时,我们使用关键字 extends 。在这种情况下,该类必须使用关键字 implements

When we define a child class, we use the keyword "extends". In this case, the class that must use the keyword "implements".

必须定义接口中声明的所有方法,参数和返回值的数量和类型均相同。

All the methods declared in the interface must be defined, with the same number and type of arguments and return value.

class myclass implements myinterface {
   public function myfunction(int $arg1, int $arg2) {
      ## implementation of myfunction;
   }
   public function mymethod(string $arg1, int $arg2) {
      # implementation of mymethod;
   }
}

Note 在接口中声明的所有方法都必须为公共方法。

Note that all the methods declared in an interface must be public.

Example

让我们定义一个名为 shape 的接口。形状具有一定的面积。你有不同几何形状的形状,如矩形、圆形等,每个形状都有一个面积,使用不同的公式计算。因此形状接口声明了一个方法 area(),该方法返回一个浮点值。

Let us define an interface called shape. A shape has a certain area. You have shapes of different geometrical appearance, such as rectangle, circle etc., each having an area, calculated with different formula. Hence the shape interface declares a method area() that returns a float value.

interface shape {
   public function area(): float;
}

接下来,我们将定义一个实现形状接口的圆形类,为了实现,该类必须提供接口中函数的具体实现。此处,圆形类中的 area() 函数计算给定半径的圆的面积。

Next, we shall define a circle class that implements shape interface, to implement, the class must provide a concrete implementation of the functions in the interface. Here, the area() function in circle class calculates the area of a circle of a given radius.

class circle implements shape {
   var $radius;
   public function __construct($arg1) {
      $this->radius = $arg1;
   }
   public function area(): float {
      return pow($this->radius,2)*pi();
   }
}

现在我们可以声明一个圆形类的对象并调用 area() 方法。

We can now declare an object of circle class, and call the area() method.

$cir = new circle(5);
echo "Radius : " . $cir->radius .  " Area of Circle: " . $cir->area(). PHP_EOL;

一个接口可以由任意数量的类(它们可能彼此无关)实现,只要实现类提供接口中每个方法的功能即可。

An interface can be implemented by any number of classes (which may be unrelated to each other) provided the implementing class provides functionality of each method in the interface.

这里有一个实现形状的 Square 类。area() 方法返回边属性的平方。

Here is a Square class that implements shape. The area() method returns the square of the side property.

class square implements shape {
   var $side;
   public function __construct($arg1) {
      $this->side = $arg1;
   }
   public function area(): float {
      return pow($this->side, 2);
   }
}

类似地,创建一个 Square 对象并调用 area() 方法。

Similarly, create a Square object and call the area() method.

Example

以下是形状接口的完整代码,由圆形和 Square 类实现 −

Given below is the complete code for a shape interface, implemented by circle and Square classes −

<?php
   interface shape {
      public function area(): float;
   }

   class square implements shape {
      var $side;
      public function __construct($arg1) {
         $this->side = $arg1;
      }
      public function area(): float {
         return pow($this->side, 2);
      }
   }
   class circle implements shape {
      var $radius;
      public function __construct($arg1) {
         $this->radius = $arg1;
      }
      public function area(): float {
         return pow($this->radius,2)*pi();
      }
   }

   $sq = new square(5);
   echo "Side: " . $sq->side .  " Area of Square: ". $sq->area() . PHP_EOL;

   $cir = new circle(5);
   echo "Radius: " . $cir->radius .  " Area of Circle: " . $cir->area(). PHP_EOL;
?>

它将生成以下 output

It will produce the following output

Side: 5 Area of Square: 25
Radius: 5 Area of Circle: 78.539816339745

Multiple Inheritance in PHP

PHP 无法建立扩展两个父类的子类。换句话说,该语句 −

PHP doesn’t have the provision to build a child class that extends two parent classes. In other words, the statement −

class child extends parent1, parent2

不受支持。但是,PHP 支持扩展一个父类并实现一个或多个接口的子类。

is not accepted. However, PHP does support having a child class that extends one parent class, and implementing one or more interfaces.

我们来看看下面的示例,该示例显示了一个扩展另一个类并实现一个接口的类。

Let use look at the following example that shows a class that extends another and implements an interface.

首先,父类标记。它具有三个实例变量或属性 $m1、$m2、$m3,分别表示三个科目中的分数。提供了一个 constructor 来初始化对象。

First, the parent class marks. It has three instance variables or properties $m1, $m2, $m3 representing the marks in three subjects. A constructor is provided to initialize the object.

class marks {
   protected int $m1, $m2, $m3;
   public function __construct($x, $y, $z) {
      $this->m1 = $x;
      $this->m2 = $y;
      $this->m3 = $z;
   }
}

我们现在提供一个名为 percent 的接口,它声明了一个方法 percent(),它应该返回一个浮点数,但没有函数体。

We now provide an interface called percent that declares a method percent(), which should return a float but doesn’t have a function body.

interface percent {
   public function percent(): float;
}

我们现在开发一个类来扩展 marks 类并在接口中为 percent() 方法提供实现。

We now develop a class that extends marks class and provides implementation for percent() method in the interface.

class student extends marks implements percent {
   public function percent(): float {
      return ($this->m1+$this->m2+$this->m3)*100/300;
   }
}

student 类继承了父构造函数,但提供了 parent() 方法的实现,该方法返回分数的百分比。

The student class inherits the parent constructor, but provides implementation of parent() method that returns the percentage of marks.

Example

完整代码如下所示:

The complete code is as follows −

<?php
   class marks {
      protected int $m1, $m2, $m3;
      public function __construct($x, $y, $z) {
         $this->m1 = $x;
         $this->m2 = $y;
         $this->m3 = $z;
      }
   }
   interface percent {
      public function percent(): float;
   }

   class student extends marks implements percent {
      public function percent(): float {
         return ($this->m1+$this->m2+$this->m3)*100/300;
      }
   }

   $s1 = new student(50, 60, 70);
   echo "Percentage of marks: ". $s1->percent() . PHP_EOL;
?>

它将生成以下 output

It will produce the following output

Percentage of marks: 60

PHP 中的接口定义了一个方法框架,用于类提供自己的不同但具体的实现。

The interface in PHP defines a framework of methods that classes use to provide a different but concrete implementation of their own.