라이브러리

[PHP] ReflectionClass::hasMethod - 메서드가 정의되어 있는지 확인합니다.




ReflectionClass::hasMethod


`ReflectionClass::hasMethod`는 PHP ReflectionClass에 속한 메소드입니다. 이 메소드는 클래스의 특정 메소드가 존재하는지 여부를 확인합니다.

사용법


`hasMethod` 메소드는 `ReflectionClass` 객체를 인자로 받습니다. 이 객체는 클래스의 정보를 담고 있는 객체입니다.

#hostingforum.kr
php

$reflectionClass = new ReflectionClass('클래스 이름');

if ($reflectionClass->hasMethod('메소드 이름')) {

    // 메소드가 존재하는 경우

} else {

    // 메소드가 존재하지 않는 경우

}



예제


#hostingforum.kr
php

class MyClass {

    public function myMethod() {

        echo 'myMethod 호출';

    }

}



$reflectionClass = new ReflectionClass('MyClass');

if ($reflectionClass->hasMethod('myMethod')) {

    echo 'myMethod 메소드가 존재합니다.';

} else {

    echo 'myMethod 메소드가 존재하지 않습니다.';

}



결과


#hostingforum.kr


myMethod 메소드가 존재합니다.



사용 예시


#hostingforum.kr
php

class MyClass {

    public function myMethod() {

        echo 'myMethod 호출';

    }

    public function myOtherMethod() {

        echo 'myOtherMethod 호출';

    }

}



$reflectionClass = new ReflectionClass('MyClass');

if ($reflectionClass->hasMethod('myMethod')) {

    echo 'myMethod 메소드가 존재합니다.';

} else {

    echo 'myMethod 메소드가 존재하지 않습니다.';

}



if ($reflectionClass->hasMethod('myOtherMethod')) {

    echo 'myOtherMethod 메소드가 존재합니다.';

} else {

    echo 'myOtherMethod 메소드가 존재하지 않습니다.';

}



결과


#hostingforum.kr


myMethod 메소드가 존재합니다.

myOtherMethod 메소드가 존재합니다.



결론


`ReflectionClass::hasMethod` 메소드는 클래스의 특정 메소드가 존재하는지 여부를 확인하는 데 사용할 수 있습니다. 이 메소드는 클래스의 정보를 담고 있는 `ReflectionClass` 객체를 인자로 받습니다.
  • profile_image
    나우호스팅 @pcs8404 

    호스팅포럼 화이팅!

    댓글목록

    등록된 댓글이 없습니다.

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

검색

게시물 검색