2016-05-17 108 views
0

有兩個參數,返回對象爲JSON如何在PHP中使用輸入變量調用函數?

我試圖讓帖子ID和連接到陣列:

// Get Considered Posts, Training, Service, Intall 
public function get_considered_posts() { 
    $item = new stdclass(); 
    $item->msg = 'Empty'; 
    $item->status = false; 
    $result = Array(); 
    $post = $get_post_by_id(156); // Training 
    if ($post) { 
     $result[] = $post ; 
    } 
    $post = $get_post_by_id(164); // Service 
    if ($post) { 
     $result[] = $post ; 
    } 

    $post = $get_post_by_id(161); // Intall 
    if ($post) { 
     $result[] = $post ; 
    } 

    if (count($result) == 0) { 
     $item->msg = 'Empty'; 
     $item->status = false; 
    } else { 
     $item->msg = 'Success'; 
     $item->status = true; 
    } 
    $item->result = $result; 
    return $item; 
} 

在這裏,我希望只使用5每一個崗位性質:

// Get Post By Id 
private function get_post_by_id($pid) { 
    $post = get_post($pid = 0); 
    if ($post) { 
     if ($post->post_status == 'publish') { 
      $object = new stdclass(); 
      $object->id = $post->ID; 
      $object->cid = $pid; 
      $object->title = $post->post_title; 
      $object->content = $post->post_content; 
      $object->image = ''.wp_get_attachment_url(get_post_thumbnail_id($post->ID)); 
      return $object; 
     } 
    } 
    return $post; 
} 

但它顯示500 Internal Server Error 當我檢查error_log它顯示:

[17-May-2016 14:27:54 UTC] PHP Notice: Undefined variable: get_post_by_id in /home/codypars/public_html/app/wp-webservice/helper.php on line 62 

[17可能 - 2016 14時27分54秒UTC] PHP致命錯誤:函數名必須是線/home/codypars/public_html/app/wp-webservice/helper.php字符串62

我該如何修復它?


解決:謝謝CD-jSJustOnUnderMillions。要在PHP類中調用自己的函數,正確的語法是:

this->myFunctionName($Param1, $Param2, $Param3, ...); 

回答

3

當您調用它們時,從函數名稱的前面刪除$。

例如

$post = $get_post_by_id(164); 

應該

$post = get_post_by_id(164); 
+1

哪裏是'$這個 - >'?? – JustOnUnderMillions

+0

是的,$ post = $ this-> get_post_by_id(156); , 謝謝 – AndroSco