2016-05-17 93 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