PHP介面

WBOY
發布: 2024-08-29 13:00:28
原創
1046 人瀏覽過

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中文網其他相關文章!

相關標籤:
php
來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!