2008-09-23 3 views
6

정적 클래스가 선언되었는지 어떻게 확인할 수 있습니까? 전 을 감안할 때 내가 확인 어떻게정적 클래스가 선언 된 경우 PHP 확인

class bob { 
    function yippie() { 
     echo "skippie"; 
    } 
} 

나중에 코드에서 클래스 :

if(is_a_valid_static_object(bob)) { 
    bob::yippie(); 
} 

그래서 내가하지 않습니다 치명적인 오류 : 클래스 '밥'은 file.php에에서 찾을 수 없습니다 라인 3

답변

13

당신은 또한 심지어 클래스를 인스턴스화하지 않고, 특정 방법의 존재를 확인할 수 있습니다

echo method_exists(bob, 'yippie') ? 'yes' : 'no'; 

당신이 Reflection API (만 PHP5)를 사용하여, 한 걸음 더 나아가 "이피은"실제로 고정되어 있는지 확인하려면

try { 
    $method = new ReflectionMethod('bob::yippie'); 
    if ($method->isStatic()) 
    { 
     // verified that bob::yippie is defined AND static, proceed 
    } 
} 
catch (ReflectionException $e) 
{ 
    // method does not exist 
    echo $e->getMessage(); 
} 

또는, 당신은 두 가지 방법을 결합 할 수

if (method_exists(bob, 'yippie')) 
{ 
    $method = new ReflectionMethod('bob::yippie'); 
    if ($method->isStatic()) 
    { 
     // verified that bob::yippie is defined AND static, proceed 
    } 
} 
관련 문제