如何用PHP中的输入变量调用函数?

时间:2016-05-17 14:37:22

标签: php wordpress function undefined

有两个参数将Object作为JSON

返回

我尝试通过ID获取帖子并附加到Array:

// 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-May-2016 14:27:54 UTC] PHP致命错误:函数名称必须是第62行/home/codypars/public_html/app/wp-webservice/helper.php中的字符串

我该如何解决?

解决了:谢谢CD-jSJustOnUnderMillions。要在PHP类中调用自己的函数,正确的语法是:

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

1 个答案:

答案 0 :(得分:3)

当你打电话时,从你的函数名前面删除$。

e.g。

$post = $get_post_by_id( 164 );

应该是

$post = get_post_by_id( 164 );