2014-04-01 2 views
-1

나는이 같은 클래스의 모양을 지금확인 전화 정적 메소드 PHP

class Foo 
{ 
    puclic static function blindPaths($paths) 
    { 
     foreach($paths as $name=>$path) 
     { 
      $method='set'.ucfirst($name).'Path'; 
      if(method_exists(????,$method)) 
       self::$method($path); 
     } 
    } 

    public function setBasePath($base) 
    { 
     //do something... 
    } 

    public function setAppPath($app) 
    { 
     //do something... 
    } 

    .... 
} 

, 난 전화 :

$paths = array(
    'base'=>'path.of.base.path', 
    'app'=>'path.of.app.path', 
    'someValue'=>'path.of.someValuePath', 
    .... 
); 
Foo::blindPaths($paths); 

문제는 때 체크 method_exists, 그 부호로 무엇을 채우기 위해 "???? " 누군가 나를 도울 수 있니? 간단한, 하나의 클래스 상황에서

+1

[TFM] (http://php.net/method_exists)에는 예제가 있습니다. – deceze

+0

^- 당신은 시인이고 그것을 알지도 못했습니다. –

답변

2
if(method_exists(__CLASS__, $method)) 
1

당신은 method_exists 호출에 대한 첫 번째 인수로 __CLASS__ 상수를 사용할 수 있지만 당신은 정적 메소드가 상위 레벨에서 정의 된 상황에 있다면 (또는

puclic static function blindPaths($paths) 
{ 
    $current = get_called_class(); 
    $current = new $current;//create instance 
    foreach($paths as $name=>$path) 
    { 
     $method='set'.ucfirst($name).'Path'; 
     if($current instanceof Foo) 
      self::$method($path); 
     elseif ($current instanceof Bar) 
      return $this->{$method}($path); 
    } 
} 
: 당신이 믹스 인터페이스와 Trait 년대를 추가하는 경우,

puclic static function blindPaths($paths) 
{ 
    $current = get_called_class(); 
    foreach($paths as $name=>$path) 
    { 
     $method='set'.ucfirst($name).'Path'; 
     if(method_exists($current,$method)) 
      self::$method($path); 
    } 
} 

을 또는 : 추상 클래스, 또는 어떤 장소의 다른는) 아마도 당신은이를 고려할 수 있습니다

그러나 어느 쪽이든, 당신의 디자인을 다시 생각해보십시오. 만약 당신이 지금 가지고있는 것과 비슷한 구조를 사용한다면, 10 번 중 9 번은 틀린 나무를 짖는 것입니다.