라이브러리

[PHP] ReflectionClass::implementsInterface - 인터페이스 구현




ReflectionClass::implementsInterface


PHP의 ReflectionClass는 클래스의 정보를 가져올 수 있는 클래스입니다. ReflectionClass::implementsInterface 메소드는 클래스가 특정 인터페이스를 구현하는지 여부를 확인하는 메소드입니다.

사용법


ReflectionClass::implementsInterface 메소드는 다음 형식으로 사용할 수 있습니다.

#hostingforum.kr
php

ReflectionClass::implementsInterface(string $interfaceName): bool



* `$interfaceName` : 인터페이스 이름을 지정합니다.

예제


다음 예제는 ReflectionClass::implementsInterface 메소드를 사용하여 클래스가 특정 인터페이스를 구현하는지 여부를 확인하는 방법을 보여줍니다.

#hostingforum.kr
php

// 인터페이스

interface Printable {

    public function print();

}



// 클래스

class Book implements Printable {

    public function print() {

        echo "책을 출력합니다.
";

    }

}



// ReflectionClass

$reflectionClass = new ReflectionClass('Book');



// 인터페이스를 구현하는지 여부를 확인합니다.

if ($reflectionClass->implementsInterface('Printable')) {

    echo "Book 클래스는 Printable 인터페이스를 구현합니다.
";

} else {

    echo "Book 클래스는 Printable 인터페이스를 구현하지 않습니다.
";

}



결과


Book 클래스는 Printable 인터페이스를 구현합니다.

사용 사례


ReflectionClass::implementsInterface 메소드는 다음 사례에서 사용할 수 있습니다.

* 클래스의 인터페이스 구현 여부를 확인하여 클래스의 역할을 판단할 수 있습니다.
* 클래스의 인터페이스 구현 여부를 확인하여 클래스의 기능을 확장할 수 있습니다.
* 클래스의 인터페이스 구현 여부를 확인하여 클래스의 호환성을 확인할 수 있습니다.

참고


ReflectionClass::implementsInterface 메소드는 PHP 5.3.0부터 사용할 수 있습니다.
  • profile_image
    나우호스팅 @pcs8404 

    호스팅포럼 화이팅!

    댓글목록

    등록된 댓글이 없습니다.

  • 전체 10,077건 / 289 페이지

검색

게시물 검색