PHP调用类中的函数

时间:2015-11-19 04:14:46

标签: php json wordpress oop json-api

我正在编写自己的PHP类,并且我在类中有多个函数。像这样:

class JSON_API_Hello_Controller {

public function test_cat(){
    global $json_api;

    $posts = $json_api->introspector->get_posts(array( 'cat' => 3));
    return $this->posts_object_result_bycat($posts);
}

public function test_cat1() {
    global $json_api;

    $posts = $json_api->introspector->get_posts(array( 'cat' => 2));
    return $this->posts_object_result_bycat($posts);
}

protected function posts_object_result_bycat($posts) {
    global $wp_query;

    return array(
        'count' => count($posts),
        'pages' => (int) $wp_query->max_num_pages,
        'posts' => $posts
    );
}

public function mix_cat(){

    $first_cat = $this->test_cat();
    $second_cat = $this->test_cat1();
   print_r($second_cat);

}

} 

我想在同一个类中的test_cat()之类的另一个函数中调用test_cat1()mix_cat()函数。每次,它都会将test_cat()test_cat1()的结果设为Null。如何拨打testcat()& test_cat1()中有mix_cat()个函数?

1 个答案:

答案 0 :(得分:0)

调用该函数的代码看起来不错($second_cat = $this->test_cat1(); print_r($second_cat);)。

如果函数在从类外部调用时返回一个值,那么必须有一些我们在这里看不到的外部变量。如果是这样,我们可能希望看到更多代码。

作为旁注,您可以通过传递2,3等的参数来组合test_cat()和test_cat1()函数。它看起来像这样:

public function test_cat1($number) {
    global $json_api;
    $posts = $json_api->introspector->get_posts(array('cat' => $number));
    return $this->posts_object_result_bycat($posts);
}
相关问题