首页  >  文章  >  后端开发  >  PHP接口

PHP接口

WBOY
WBOY原创
2024-08-29 13:00:281057浏览

PHP 接口帮助我们编写有用的程序,指示必须执行的类的公共方法,甚至不包括复杂性以及我们的特定方法将如何实现。 PHP 接口只会定义参数和名称,而不会定义方法的内容,任何实现该接口的类都需要实现该接口定义的所有方法。

广告 该类别中的热门课程 PHP 开发人员 - 专业化 | 8 门课程系列 | 3次模拟测试

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

这些接口与类类似,但只有类短语会在声明中被接口关键字替换。

语法:

<?php
Interface interface_name
{
//List of methods
}
?>

为什么我们需要 PHP 接口?

它可以在同一个类中实现一个接口,但也可以在同一个类中创建多个接口。类可以使用 PHP Object 接口实现特定方法,而不受如何实现它们的限制。接口与类相同或相似,但PHP的interface关键字取代了class关键字,而不使用任何定义了其内容的方法。您可以在接口中声明构造函数。需要时,一个或多个类可以实现多个接口,用逗号分隔每个接口。

您可以使用“扩展”操作来扩展 PHP 编程语言中的类。 PHP 接口是下一个抽象级别。它与抽象类类似,但略有不同。接口或接口允许您创建代码,这有助于定义类的方法,但您甚至不能向这些方法添加任何代码,而抽象类允许与 PHP 接口或接口相同或相似的事情但在抽象类中,可以将代码添加到一个或多个方法中。

使用 PHP 接口

PHP 5.3.9 之前的旧版本甚至无法实现两个同名的接口,因为这会导致一些歧义。最重要的是,最新的 PHP 版本允许具有相同/相似签名的重复方法。实现该接口的类应使用与“里氏替换原则 - LSP”兼容的方法签名。如果不使用方法签名,结果将会出现致命错误。

该类将使用接口向其方法添加内容,仅由方法组成,没有任何预先存在的内容。接口中的方法大多对公众可见,没有任何限制。 PHP 接口与类不同/相似。类可以继承许多/多个接口,但类一次只能继承一个类。它也有这个优点。变量存在于接口内部。

PHP 接口示例

以下是 PHP 接口的示例:

示例#1

在下面的示例中,我们声明了一个名为“MyInterfaceName1”的接口,仅提到了两个方法。它们是接口内部的method11和method21,没有使用任何内容。然后名为“MyClassName1”的类将实现接口“MyInterfaceName1”并根据需求使用可用的方法。

正如定义所说,接口也由没有内容的方法组成。在接口的实现过程中,我们只在课堂上讨论相关内容。有时,人们将这些接口方法称为抽象方法。

代码:

<?php
interface MyInterfaceName1{
public function method11();
public function method21();
}
class MyClassName1 implements MyInterfaceName1{
public function method11(){
echo "Now Method11 Called" . "<br>";
}
public function method21(){
echo "Now Method21 Called". "\n";
}
}
$obj = new MyClassName1;
$obj->method11();
$obj->method21();
?>

输出:

PHP接口

示例#2

下面的程序包含两个接口,每个接口都有自己的方法。然后使用名为 info() 的方法创建一个 Bird 类。现在,Dove 类扩展为 Bird 类,它实现了 CanFly PHP 接口来获取打印语句,因为打印语句/其他不会出现在接口内,因为它只有方法。

根据 Bird 的特点制作了 2 个接口,“CanFly”和“CanSwim”各有 1 个方法。它们是 Fly() 和 Swim()。 “Bird”类中的方法“info()”打印/回显语句以确定鸟的类型以及它是否确实是鸟。扩展“bird”类,实现“CanFly”和“CanSwim”接口,将Dove、Penguin、Duck等鸟类的详细信息一一放入“$name1”变量中。另外,为接口添加 echo 语句。

We have created a function named “describe($bird)” that utilizes the “CanFly” and “CanSwim” interfaces to determine if a creature is a bird. If the $ bird doesn’t satisfy the fly() and swim(), the else condition exists in the PHP program’s output. Now calling all the described functions with the 3 bird classes. You will get the output of the 3 birds as needed. Check out below. It is a simple description, but you will know how it is happening if you look at the code and output of the PHP Program below.

Code:

<?php
/**
* This is An example of the duck typing in the PHP Script
*/
interface CanFly1 {
public function fly1();
}
interface CanSwim1 {
public function swim1();
}
class Bird1 {
public function info1() {
echo "I am a {$this->name1}\n";
echo "I am a bird\n";
}
}
/**
* This is some of the implementations of the birds
*/
class Dove1 extends Bird1 implements CanFly1 {
var $name1 = "Dove";
public function fly1() {
echo "I fly\n";
}
}
class Penguin1 extends Bird1 implements CanSwim1 {
var $name1 = "Penguin";
public function swim1() {
echo "I swim\n";
}
}
class Duck1 extends Bird1 implements CanFly1, CanSwim1 {
var $name1 = "Duck";
public function fly1() {
echo "I fly\n";
}
public function swim1() {
echo "I swim\n";
}
}
/**
* This is one of the simple function which is to describe a bird
*/
function describe1($bird1) {
if ($bird1 instanceof Bird1) {
$bird1->info1();
if ($bird1 instanceof CanFly1) {
$bird1->fly1();
}
if ($bird1 instanceof CanSwim1) {
$bird1->swim1();
}
} else {
die("This is not a bird. I cannot describe it.");
}
}
// Now describing the birds
describe1(new Penguin1);
echo "---\n<br>";
describe1(new Dove1);
echo "---\n<br>";
describe1(new Duck1);

Output:

PHP接口

Advantages

Following are some of the advantages given.

  • It will allow the unrelated classes to implement similar methods without considering their positions in the class with an inheritance hierarchy.
  • We can model many inheritances in this approach by implementing multiple interfaces in a class, but the class can only extend one class.
  • It can implement an inheritance which can save the caller from the implementation of the object methods fully. This implementation of the heritance concept is also helpful to focus on the object’s interface so that the caller interface doesn’t even affect it.

Recommended Article

This is a guide to the PHP Interface. Here we discuss the PHP Interface, its Advantages, and its various examples to understand the PHP Interface concept briefly. You can also go through our other suggested articles to learn more –

  1. PHP Encryption
  2. PHP Frameworks
  3. Abstract Class in PHP
  4. PHP Superglobal Variables

以上是PHP接口的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn