ホームページ  >  記事  >  バックエンド開発  >  PHPインターフェース

PHPインターフェース

WBOY
WBOYオリジナル
2024-08-29 13:00:281057ブラウズ

PHP インターフェイスは、複雑さや特定のメソッドの実装方法を含めずに、実行する必要があるクラスのパブリック メソッドを示す便利なプログラムの作成に役立ちます。 PHP インターフェイスは、メソッドの内容ではなく、引数と名前のみを定義します。インターフェイスを実装するクラスは、インターフェイスによって定義されたすべてのメソッドを実装する必要があります。

広告 このカテゴリーの人気コース PHP 開発者 - 専門分野 | 8コースシリーズ | 3 つの模擬テスト

無料ソフトウェア開発コースを始めましょう

Web 開発、プログラミング言語、ソフトウェア テスト、その他

これらのインターフェイスはクラスに似ていますが、クラス句のみが宣言内のインターフェイス キーワードに置き換えられます。

構文:

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

PHP インターフェースが必要な理由

同じクラス内に 1 つのインターフェイスを実装できますが、同じクラス内にさらに多くのインターフェイスを作成できます。クラスは、実装方法の制限を受けることなく、PHP オブジェクト インターフェイスを使用して特定のメソッドを実装できます。インターフェイスはクラスと同じか類似していますが、内容が定義されたメソッドを使用せずに、PHP のインターフェイス キーワードがクラス キーワードを置き換えます。インターフェイス内でコンストラクターを宣言できます。必要に応じて、各インターフェイスをカンマで区切ることにより、クラスで複数のインターフェイスを実装できます。

「extends」操作を使用して、PHP プログラミング言語のクラスを拡張できます。 PHP インターフェイスは、次のレベルの抽象化です。抽象クラスと似ていますが、若干の違いがあります。インターフェイスを使用すると、クラスのメソッドを定義するのに役立つコードを作成できますが、それらのメソッドにコードを追加することさえできません。一方、抽象クラスでは、PHP インターフェイスと同じまたは類似したものを使用できます。しかし、抽象クラスでは、メソッドにコードを追加できます。

PHP インターフェースの作業

PHP 5.3.9 より前の古いバージョンでは、あいまいさが生じるため、同じ名前を持つ 2 つのインターフェイスだけを実装することさえできませんでした。何よりも、最近の PHP バージョンでは、同じ/類似したシグネチャを持つ重複メソッドが許可されています。インターフェイスを実装するクラスは、「Liskov Substitution Principle – LSP」と互換性のあるメソッド シグネチャを使用する必要があります。メソッド シグネチャが使用されていない場合、結果に致命的なエラーが発生します。

クラスはインターフェースを使用してメソッドにコンテンツを追加します。これは、既存のコンテンツを含まないメソッドのみで構成されます。インターフェイス内のメソッドのほとんどは制限なく公開されています。 PHP インターフェイスはクラスと同じ/類似していません。クラスは多数または複数のインターフェイスを継承できますが、クラスは一度に 1 つのクラスのみを継承できます。こんなメリットもあります。変数はインターフェイス内に存在します。

PHP インターフェースの例

以下は PHP インターフェースの例です:

例 #1

以下の例では、2 つのメソッドのみを指定して「MyInterfaceName1」という名前のインターフェイスを宣言しました。コンテンツを使用しないインターフェイス内のメソッド 11 とメソッド 21 です。次に、「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

以下のプログラムには 2 つのインターフェイスが含まれており、それぞれに独自のメソッドがあります。次に、info() というメソッドを使用してクラス Bird を作成します。ここで、クラス Dove はクラス Bird に拡張されます。このクラスは、CanFly PHP インターフェイスを実装して、印刷ステートメントを取得します。これは、インターフェース内に print ステートメント/その他が存在しないためです。これはメソッドのみを持つためです。

Bird 2 のインターフェースの特性に基づいて、「CanFly」と「CanSwim」がそれぞれ 1 つのメソッドで作成されます。それは fly() と swim() です。クラス「Bird」のメソッド「info()」は、鳥の種類とそれが本当に鳥であるかどうかを判断するステートメントを出力/エコーします。 「bird」クラスを拡張し、「CanFly」および「CanSwim」インターフェイスを実装して、ハト、ペンギン、アヒルなどの鳥の詳細を 1 つずつ「$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 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
前の記事:PHP zipアーカイブ次の記事:PHP zipアーカイブ