2014-02-12 3 views
0

나는 일부 jsons를 반환하거나 저장하기 위해 PHP 함수를 만들었고 클래스는 다음과 같이 보입니다. 무엇 궁금문자열에서 php 함수 이름을 호출합니다.

<?php 
    class calendarModel { 

     // global variables 
     var $user; 
     var $action; 
     var $connect; 

     // init class 
     function __construct($action = "getEvents") { 
      $this->user = 1; 

      $this->action = $action; 

      $dbhost = "localhost"; 
      $dbport = "5432"; 
      $dbname = "fixevents"; 
      $dbuser = "postgres"; 
      $dbpass = "123"; 
      $this->connect = pg_connect("host=" . $dbhost . " port=" . $dbport . " dbname=" . $dbname . " user=" . $dbuser . " password=" . $dbpass); 

      $this->executeAction(); 
     } 

     // action router 
     function executeAction() { 
      if($this->action == "getEvents") 
       $this->getEvents(); 
      else if($this->action == "moveEvent") 
       $this->moveEvent(); 
      else if($this->action == "insertEvent") 
       $this->insertEvent(); 
      else if($this->action == "updateEvent") 
       $this->updateEvent(); 
      else if($this->action == "getCalendars") 
       $this->getCalendars(); 
      else if($this->action == "toggleCalendar") 
       $this->toggleCalendar(); 
      else if($this->action == "deleteCalendar") 
       $this->deleteCalendar(); 
      else if($this->action == "insertCalendar") 
       $this->insertCalendar(); 
     } 

     // getEvents 
     function getEvents() { 
      //... 
     } 

     // moveEvent 
     function moveEvent() { 
      //... 
     } 

     // insertEvent 
     function insertEvent() { 
      //... 
     } 

     // updateEvent 
     function updateEvent() { 
      //... 
     } 

     // toggleCalendar 
     function toggleCalendar() { 
      //... 
     } 

     // deleteCalendar 
     function deleteCalendar() { 
      //... 
     } 

     // insertCalendar 
     function insertCalendar() { 
      //... 
     } 

    } 

    // call class 
    if(isset($_GET['action'])) 
     $instance = new calendarModel($_GET['action']); 
    else 
     $instance = new calendarModel(); 
?> 

, 내가 어떻게 든/다른 기능이 executeAction라고하면 경우 대신 그 큰 수 있도록 문자열 이름에서 contruct에서 작업을 호출 할 수 있습니다. 미리 감사드립니다, 대니얼!

+3

'call_user_func은 ()' – zerkms

+0

@zerkms'$ this'는 일을 복잡하게 만듭니다. – kapa

+0

@kapa :'call_user_func (array ($ this, 'methodname'))' – zerkms

답변

0

바머가 거의 정확합니다. $ this -> {$ action}();

+0

대단히 감사합니다.이 기능이 완벽하게 작동합니다. 이제 더 이상 Action 함수를 실행하지 않아도됩니다. 그것은 –

+1

과 같은 것입니다.이 답변은 확인해 볼 가치가 없습니다. 미안합니다. – zerkms

+0

'$ action'은 클래스 속성입니다.'$ this-> ' – Barmar

4

당신이 함수 이름이 사용되는 표현을 사용하는 경우, 표현식의 값이 함수의 이름으로 사용됩니다

사용자 입력에서 작업을 얻고 있기 때문에
function executeAction() { 
    $this->{$this->action}(); 
} 

, 확인 당신이 그것을 확인합니다. 그렇지 않으면 누군가가 임의의 메소드를 실행할 수있는 입력을 보낼 수 있습니다.

+0

고맙게도 의도 한대로 작동합니다. D 저는 여러분 모두에게 정확한 대답을 줄 수 있기를 바랍니다. –

1
같은

사용 무언가 : $ 동작이 사용자에 의해 정의된다

function __construct($action = "getEvents") 
{ 
    ... 
    $this->$action(); 
} 

, 당신은 $ 동작이 클래스의 기존 기능 여부를 확인 할 수 있습니다 :

if (array_key_exists($action, get_class_methods($this))) { 
    $this->$action(); 
} 
관련 문제