PHPのお勉強



インターフェース@PHP

インタフェースを作成します。

抽象クラスを似ています。
インターフェースの場合は、宣言のみです。
実装はできません。

interface修飾子で宣言します。
あとは、メソッドなどを宣言します。

インターフェースの実装クラスを作成します。
implementsにより、実装します。
継承ではないです。

実装したクラスを作成したら、
生成して、利用します。


<?php

    //インターフェース
    interface TestInterface {

        //コンスト
        const STR_CONST = "AAA";

        //methodA
        public function methodA();

        //methodA
        public function methodB($str);
    }

    //インターフェースを実装したクラス
    class Test implements TestInterface {
        
        //methodAを実装
        public function methodA() {
            return self::STR_CONST;
        }
        
        //methodBを実装
        public function methodB($str) {
            print $str;
        }
    }
    
    //Testクラス生成
    $test = new Test();
    
    //methodA
    print $test->methodA();

    //methodB
    $test->methodB("BBB");

?>




Copyright (C) PHPのお勉強. All Rights Reserved.