
method_exists 함수는 PHP에서 메서드 존재 여부를 확인하는 데 사용됩니다. 이 함수는 메서드 이름을 인수로 받으며, 메서드가 클래스에 존재하는지 여부를 boolean 값으로 반환합니다.
method_exists 함수는 접근자, 수정자, 생성자, 소멸자도 포함하여 메서드 존재 여부를 확인합니다. 예를 들어, 다음 코드는 method_exists 함수를 사용하여 메서드 존재 여부를 확인하는 방법을 보여줍니다.
#hostingforum.kr
php
class MyClass {
public function __construct() {}
public function myMethod() {}
public function getMyProperty() {}
public function setMyProperty($value) {}
}
$obj = new MyClass();
echo method_exists($obj, '__construct') ? 'true' : 'false'; // true
echo method_exists($obj, 'myMethod') ? 'true' : 'false'; // true
echo method_exists($obj, 'getMyProperty') ? 'true' : 'false'; // true
echo method_exists($obj, 'setMyProperty') ? 'true' : 'false'; // true
method_exists 함수는 클래스 이름을 인수로 받을 수도 있습니다. 이 경우, 클래스가 존재하는지 여부를 boolean 값으로 반환합니다. 예를 들어, 다음 코드는 method_exists 함수를 사용하여 클래스 존재 여부를 확인하는 방법을 보여줍니다.
#hostingforum.kr
php
echo method_exists('stdClass', 'stdClass') ? 'true' : 'false'; // true
echo method_exists('stdClass', 'stdClass::myMethod') ? 'true' : 'false'; // false
method_exists 함수는 클래스 이름을 인수로 받을 때, 클래스 이름 뒤에 '::'를 사용하여 메서드 이름을 지정해야 합니다. 만약 메서드 이름을 지정하지 않으면, method_exists 함수는 클래스 이름을 인수로 받은 것으로 간주합니다.
method_exists 함수는 PHP 4에서 사용할 수 있었지만, PHP 5에서 deprecated되었으며, PHP 7에서 삭제되었습니다. 대신, is_callable 함수를 사용하여 메서드 존재 여부를 확인할 수 있습니다. 예를 들어, 다음 코드는 is_callable 함수를 사용하여 메서드 존재 여부를 확인하는 방법을 보여줍니다.
#hostingforum.kr
php
class MyClass {
public function __construct() {}
public function myMethod() {}
public function getMyProperty() {}
public function setMyProperty($value) {}
}
$obj = new MyClass();
var_dump(is_callable(array($obj, '__construct'))); // bool(true)
var_dump(is_callable(array($obj, 'myMethod'))); // bool(true)
var_dump(is_callable(array($obj, 'getMyProperty'))); // bool(true)
var_dump(is_callable(array($obj, 'setMyProperty'))); // bool(true)
is_callable 함수는 메서드 이름을 인수로 받을 수도 있으며, 메서드가 존재하는지 여부를 boolean 값으로 반환합니다. 만약 메서드 이름을 지정하지 않으면, is_callable 함수는 클래스 이름을 인수로 받은 것으로 간주합니다.
2025-08-05 05:06