2016-10-17 2 views
0

클래스 기능을 반향시킬 수 있습니까? 나는 이것을 시도했지만 오류가 발생한다.클래스 기능을 에코하는 방법은 무엇입니까?

<input type="text" name="query"> 
ex. empid('1'); 

    <?php 
    include 'class/class.sample.php'; 
    $sampleObj = new sample(); 
    $function = $_POST['query']; //ex empid('1'); 
    echo $drObj->$function; 
    ?> 
+1

무엇이 오류입니까? –

+0

'echo $ drObj -> {$ function}; 시도해보십시오. – devpro

+0

멋집니다! 그것은 누구나 해당 객체에 대한 임의의 인수를 사용하여 임의의 메소드를 실행할 수 있습니다! 예를 들어 소멸자! – arkascha

답변

0

나는 그걸 그대로 전달할 수 있다고 생각하지 않지만 달성 할 수 없다는 것을 의미하지는 않습니다. 원하는 함수를 호출하는 데 사용할 수있는 값으로 입력을 구문 분석 할 작은 함수를 작성할 수 있습니다. 이

<?php 
// I've commented your include because it doesn't benefit this example 
//include 'class/class.sample.php'; 
$sampleObj = new sample(); 
//$function = $_POST['query']; //ex empid('1'); 

// set input hardcoded for the benefit of this example 
$output = fetchFunction("empid('1','2','3')"); 
call_user_func_array(array($sampleObj, $output['function']), $output['parameters']); 

/** 
* Use this function to parse input like "empid('1','2')" 
* to array('function'=>'empid',parameters=>array(1,2)) 
* This can be used in a call_user_func_array 
* 
* @param $input 
* @return array 
*/ 
function fetchFunction($input){ 
    // match inside() to get the parameters 
    preg_match('/(?<=\()(.+)(?=\))/is', $input, $parameters); 

    return array(
     'parameters' => explode(',', str_replace(array('\'','"'),'',$parameters[0])), 
     'function' => substr($input,0,strpos($input,'(')) 
    ); 
} 


class sample{ 
    public function empid($id){ 
     print_r(func_get_args()); 
    } 
} 

같은 뭔가 내가 출력을 보여 더미 클래스를 추가 한, 및 (method_exists 같은 뭔가 가능) 예를 들어 기존 기능에 대한 검사를 포함하지 않았다. 당신은 자신의 클래스 설정과 기능을 사용하기 위해 그것을 변경해야 할 것입니다.

관련 문제